Usage of SET DATEFIRST in SQL SERVER

435 阅读1分钟

SET DATEFIRST (Transact-SQL)

说明:

将一周的第一天设置为1到7之间的数字,默认是7。

语法:

SET DATEFIRST { number | @number_var }

参数:

number | @number_var
是一个整数,指示一周中的第一天。 它可以是以下值之一。

一周的第一天是
1星期一
2星期二
3星期三
4星期四
5星期五
6星期六
7(默认值)星期日

备注:

若要查看SET DATEFIRST的当前设置,请使用@@ DATEFIRST函数。
如:SELECT @@DATEFIRST

The setting of SET DATEFIRST is set at execute or run time and not at parse time.  
SET DATEFIRST的设置是在执行或运行时设置的,而不是在解析时设置的。  

Specifying SET DATEFIRST has no effect on DATEDIFF. DATEDIFF always uses Sunday as the first day of the week to ensure the function is deterministic.  
指定SET DATEFIRST对DATEDIFF无效。DATEDIFF始终使用星期日作为一周的第一天,以确保该函数具有确定性。

例子:

SET DATEFIRST 1
SELECT DATEPART(WEEKDAY, SYSDATETIME());


SET DATEFIRST 7
SELECT DATEPART(WEEKDAY, SYSDATETIME());

参考:

微软文档地址