Transact-SQL 参考

SET DATEFIRST

将一周的第一天设置为从 1 到 7 之间的一个数字。

语法

SET DATEFIRST { number | @number_var }

参数

number | @number_var

是一个整数,表示一周的第一天,可以是下列值中的一个。

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

注释

使用 @@DATEFIRST 函数检查 SET DATEFIRST 的当前设置。

SET DATEFIRST 的设置是在执行或运行时设置,而不是在分析时设置。

权限

SET DATEFIRST 权限默认授予所有用户。

示例

下例显示一个日期值对应的一周中的某一天,并显示更改 DATEFIRST 设置的效果。

-- SET DATEFIRST to U.S. English default value of 7.
SET DATEFIRST 7
GO
SELECT CAST('1/1/99' AS datetime), DATEPART(dw, '1/1/99')
-- January 1, 1999 is a Friday. Because the U.S. English default 
-- specifies Sunday as the first day of the week, DATEPART of 1/1/99 
-- (Friday) yields a value of 6, because Friday is the sixth day of the 
-- week when starting with Sunday as day 1.
SET DATEFIRST 3
-- Because Wednesday is now considered the first day of the week,
-- DATEPART should now show that 1/1/99 (a Friday) is the third day of the -- week. The following DATEPART function should return a value of 3.
SELECT CAST('1/1/99' AS datetime), DATEPART(dw, '1/1/99')
GO

请参见

数据类型

@@DATEFIRST

datetime 和 smalldatetime

SET