SET DATEFIRST (Transact-SQL)
將一週的第一天設為 1-7 其中一個數字。
如需所有 Transact-SQL 日期和時間資料類型與函數的概觀,請參閱<日期和時間函數 (Transact-SQL)>。如需日期和時間資料類型與函數常用的資訊和範例,請參閱<使用日期和時間資料>。
語法
SET DATEFIRST { number | @number_var }
引數
number | **@**number_var
這是一個整數,代表一週的第一天。它可以是下列值之一。值
每週的第一天是
1
星期一
2
星期二
3
星期三
4
星期四
5
星期五
6
星期六
7 (預設值,U.S. English)
星期日
權限
需要 public 角色中的成員資格。
範例
下列範例會顯示每週日期來作為日期值,且會顯示變更 DATEFIRST 設定的作用。
-- SET DATEFIRST to U.S. English default value of 7.
SET DATEFIRST 7;
SELECT CAST('1999-1-1' AS datetime2) AS SelectDate
,DATEPART(dw, '1999-1-1') AS DayOfWeek;
-- January 1, 1999 is a Friday. Because the U.S. English default
-- specifies Sunday as the first day of the week, DATEPART of 1999-1-1
-- (Friday) yields a value of 6, because Friday is the sixth day of the
-- week when you start with Sunday as day 1.
SET DATEFIRST 3;
-- Because Wednesday is now considered the first day of the week,
-- DATEPART now shows that 1999-1-1 (a Friday) is the third day of the
-- week. The following DATEPART function should return a value of 3.
SELECT CAST('1999-1-1' AS datetime2) AS SelectDate
,DATEPART(dw, '1999-1-1') AS DayOfWeek;
GO