将周开始设置为星期一 - datefirst无效

时间:2012-03-23 10:08:17

标签: sql sql-server sql-server-2008

考虑以下脚本:

SET LANGUAGE 'German'
GO
SET DATEFIRST 1
GO

DECLARE @FullDate DATETIME
DECLARE @DayofWeek tinyint
DECLARE @relativeWeek int

SELECT @FullDate  = '5/1/2008'
WHILE (@FullDate <= '7/1/2008')
BEGIN

SELECT DATEPART (DW , @FullDate) as day_of_week, DATEDIFF(WEEK, CURRENT_TIMESTAMP, @FullDate) as relative_week

SELECT @FullDate = DATEADD(DAY, 1, @Fulldate)
END

目前的结果是

day_of_week   relative_week
----------------------------
6   -220
7   -219
1   -219

现在我如何设法得到这样的结果:

day_of_week   relative_week
----------------------------
6   -220
7   -220 <-- here's the difference :) 
1   -219

意思是在星期一/星期一的第一周开始。如您所见,将datefirst变量设置为1没有效果。

提前致谢。

1 个答案:

答案 0 :(得分:3)

请参阅:http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/8cc3493a-7ae5-4759-ab2a-e7683165320b/

希望它有所帮助。

事实上 - DATEDIFF并不关心DATEFIRST