@@DATEFIRST (Transact-SQL)@@DATEFIRST (Transact-SQL)

適用於: 是SQL Server 是Azure SQL Database 是Azure SQL 資料倉儲 是平行處理資料倉儲 APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure SQL Data Warehouse yesParallel Data Warehouse

此函式會傳回特定工作階段之 SET DATEFIRST 的目前值。This function returns the current value of SET DATEFIRST, for a specific session.

如需所有 Transact-SQLTransact-SQL 日期和時間資料類型與函式的概觀,請參閱日期和時間資料類型與函式 (Transact-SQL)See Date and Time Data Types and Functions (Transact-SQL) for an overview of all Transact-SQLTransact-SQL date and time data types and functions.

主題連結圖示 Transact-SQL 語法慣例Topic link icon Transact-SQL Syntax Conventions

語法Syntax

@@DATEFIRST  

傳回類型Return Type

tinyinttinyint

RemarksRemarks

SET DATEFIRST n 會指定當週第一天 (星期日、星期一、星期二等)。SET DATEFIRST n specifies the first day (SUNDAY, MONDAY, TUESDAY, etc.) of the week. n 值的範圍從 1 到 7。The value of n ranges from 1 to 7.

SET DATEFIRST 3;
GO  
SELECT @@DATEFIRST; -- 3 (Wednesday)
GO

針對美國英文環境,@@DATEFIRST 預設為 7 (星期日)。For a U.S. English environment, @@DATEFIRST defaults to 7 (Sunday).

這項語言設定會影響字元字串解譯,因為 SQL Server 會將這些字串轉換成日期值以便資料庫儲存。This language setting impacts character string interpretation as SQL Server converts those strings to date values for database storage. 這項設定也會影響儲存在資料庫中之日期值的顯示。This setting also impacts display of date values stored in the database. 這項設定不會影響日期資料的儲存格式。This setting does not impact the storage format of date data.

此範例會先將語言設定為 ItalianThis example first sets the language to Italian. 陳述式 SELECT @@DATEFIRST; 會傳回 1The statement SELECT @@DATEFIRST; returns 1. 下一個陳述式會接著將語言設定為 us_englishThe next statement sets the language to is then set to us_english. 最後一個陳述式 SELECT @@DATEFIRST; 會傳回 7The final statement, SELECT @@DATEFIRST; returns 7.

SET LANGUAGE Italian;  
GO  
SELECT @@DATEFIRST;  
GO  
SET LANGUAGE us_english;  
GO  
SELECT @@DATEFIRST;  

範例Examples

此範例是將當週第一天設為 5 (星期五),並且假設當日 (Today) 是星期六。This example sets the first day of the week to 5 (Friday), and assumes that the current day, Today, falls on Saturday. SELECT 陳述式會傳回 DATEFIRST 值以及當週當日的號碼。The SELECT statement returns the DATEFIRST value and the number of the current day of the week.

SET DATEFIRST 5;  
SELECT @@DATEFIRST AS 'First Day'  
    ,DATEPART(dw, SYSDATETIME()) AS 'Today';  

以下為結果集:Here is the result set.

First Day         Today  
----------------  --------------  
5                 2  

範例Example

Azure SQL 資料倉儲Azure SQL Data Warehouse 平行處理資料倉儲Parallel Data Warehouseand 平行處理資料倉儲Parallel Data Warehouse

SELECT @@DATEFIRST;  

另請參閱See also

組態函式 (Transact-SQL)Configuration Functions (Transact-SQL)