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
请参见数据类型@@DATEFIRSTdatetime 和 smalldatetimeSET©1988-2000 Microsoft Corporation。保留所有权利。
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
请参见数据类型@@DATEFIRSTdatetime 和 smalldatetimeSET©1988-2000 Microsoft Corporation。保留所有权利。
@@DATEFIRST
返回 SET DATEFIRST 参数的当前值,SET DATEFIRST 参数指明所规定的每周第一天:1 对应星期一,2 对应星期二,依次类推,用 7 对应星期日。语法
@@DATEFIRST返回类型
tinyint注释
美国英语中默认 7 对应星期日。示例
下面的示例将每周第一天设为 5 (星期五),并假定当日是星期六。SELECT 语句返回 DATEFIRST 值和当日是此周的第几天。SET DATEFIRST 5
SELECT @@DATEFIRST AS '1st Day', DATEPART(dw, GETDATE()) AS 'Today'下面是结果集。从星期五算起,今天(星期六)是第二天。1st Day Today
---------------- --------------
5 2
请参见DATEPART配置函数SET DATEFIRST©1988-2000 Microsoft Corporation。保留所有权利。
第15周的最后一天(周日)为什么和第14周的第二天(周二)相差两周呢?怎样才能让它们相差一周?
set datefirst 1
select datepart(week,'2003-4-13')
select datepart(week,'2003-4-1')
select datediff(week,'2003-4-1','2003-4-13')
set datefirst 1
select datepart(week,'2003-4-13')-select datepart(week,'2003-4-1')
也可以考虑在日期上做处理
declare @da1 datetime,@da2 datetime
select @da1='2003-4-1',@da2='2003-4-13'
select datediff(week,@da1-1,@da2-1)