SET DATEFIRST (Transact-SQL)

將一週的第一天設為 1-7 其中一個數字。

如需所有 Transact-SQL 日期和時間資料類型與函數的概觀,請參閱<日期和時間資料類型與函數 (Transact-SQL)>。

適用於:SQL Server (SQL Server 2008 透過目前版本)、Windows Azure SQL 資料庫 (初始版本,透過目前版本)。

主題連結圖示 Transact-SQL 語法慣例

語法

SET DATEFIRST { number | @number_var } 

引數

  • number | **@**number_var
    這是一個整數,代表一週的第一天。 它可以是下列值之一。

    每週的第一天是

    1

    星期一

    2

    星期二

    3

    星期三

    4

    星期四

    5

    星期五

    6

    星期六

    7 (預設值,U.S. English)

    星期日

備註

若要查看 SET DATEFIRST 的目前設定,請使用 @@DATEFIRST 函數。

SET DATEFIRST 的設定是在執行階段進行設定,而不是在剖析階段進行設定。

指定 SET DATEFIRST 對 DATEDIFF 沒有任何作用。 DATEDIFF 一定會使用星期天當做一週的第一天,以確保此函數具決定性。

權限

需要 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

請參閱

參考

SET 陳述式 (Transact-SQL)