SET DATEFIRST (Transact-SQL)

適用於: SQL ServerAzure SQL DatabaseAzure SQL 受控執行個體Azure Synapse AnalyticsAnalytics Platform System (PDW)

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

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

Transact-SQL 語法慣例

Syntax

SQL Server 和 Azure SQL Database 的語法

SET DATEFIRST { number | @number_var }   

Azure Synapse Analytics 和平行處理資料倉儲的語法

SET DATEFIRST 7 ;  

注意

若要檢視 SQL Server 2014 (12.x) 和舊版的 Transact-SQL 語法,請參閱 舊版檔

引數

number | @number_var

這是一個整數,代表一週的第一天。 它可以是下列值之一。

每週的第一天是
1 星期一
2 Tuesday
3 星期三
4 Thursday
5 星期五
6 星期六
7 (預設值,美式英文) 星期日

備註

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

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

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

如同所有 SET 語句,SET DATEFIRST 會影響目前的會話。

權限

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