開始我有這個功能非常適用於計算一個月的一週 - 我需要它開始的一週從週一計算每週週一
CREATE FUNCTION dbo.ufs_FirstofMonth (@theDate DATETIME)
RETURNS DATETIME
AS
BEGIN
RETURN (DATEADD(d, (DAY(@theDate)-1) * (-1) ,@theDate))
END
GO
CREATE FUNCTION dbo.ufs_FirstSunday (@theDate DATETIME)
RETURNS DATETIME
AS
BEGIN
RETURN (DATEADD(d, CASE WHEN DATEPART (dw , dbo.ufs_FirstofMonth(@theDate)) = 1 THEN 0
ELSE 8-DATEPART (dw , dbo.ufs_FirstofMonth(@theDate))
END
, dbo.ufs_FirstofMonth(@theDate)))
END
GO
CREATE FUNCTION dbo.ufs_WeekOfMonth (@theDate DATETIME)
RETURNS INTEGER
AS
BEGIN
RETURN (CASE WHEN DATEPART (dw , @theDate) > DAY(@theDate)
THEN 1 + DATEDIFF(wk, dbo.ufs_FirstSunday(DATEADD(mm,-1,@theDate)) , @theDate)
ELSE 1 + DATEDIFF(wk, dbo.ufs_FirstSunday(@theDate) , @theDate)
END
)
END
第一個代碼完美工作 - 謝謝! – user2871996