SET DATEFIRST (Transact-SQL)SET DATEFIRST (Transact-SQL)
適用対象:Applies to: SQL ServerSQL Server (サポートされているすべてのバージョン)
SQL ServerSQL Server (all supported versions)
Azure SQL データベースAzure SQL Database
Azure SQL データベースAzure SQL Database
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure Synapse AnalyticsAzure Synapse Analytics
Azure Synapse AnalyticsAzure Synapse Analytics
Parallel Data WarehouseParallel Data Warehouse
Parallel Data WarehouseParallel Data Warehouse
SQL ServerSQL Server (サポートされているすべてのバージョン)
SQL ServerSQL Server (all supported versions)
Azure SQL データベースAzure SQL Database
Azure SQL データベースAzure SQL Database
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure Synapse AnalyticsAzure Synapse Analytics
Azure Synapse AnalyticsAzure Synapse Analytics
Parallel Data WarehouseParallel Data Warehouse
Parallel Data WarehouseParallel Data Warehouse
週の最初の曜日を 1 から 7 の数値で設定します。Sets the first day of the week to a number from 1 through 7.
すべての Transact-SQLTransact-SQL 日付および時刻のデータ型と関数の概要については、「日付と時刻のデータ型および関数 (Transact-SQL)」を参照してください。For an overview of all Transact-SQLTransact-SQL date and time data types and functions, see Date and Time Data Types and Functions (Transact-SQL).
Transact-SQL 構文表記規則
Transact-SQL Syntax Conventions
構文Syntax
-- Syntax for SQL Server and Azure SQL Database
SET DATEFIRST { number | @number_var }
-- Syntax for Azure Synapse Analytics and Parallel Data Warehouse
SET DATEFIRST 7 ;
注意
SQL Server 2014 以前の Transact-SQL 構文を確認するには、以前のバージョンのドキュメントを参照してください。To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.
引数Arguments
number | @ number_varnumber | @number_var
週の最初の曜日を示す整数値を指定します。Is an integer that indicates the first day of the week. 次のいずれかの値を指定できます。It can be one of the following values.
値Value | 週の最初の曜日First day of the week is |
---|---|
11 | 月曜日Monday |
22 | TuesdayTuesday |
33 | 水曜日Wednesday |
44 | ThursdayThursday |
55 | 金曜日Friday |
66 | 土曜日Saturday |
7 (米国英語、既定値)7 (default, U.S. English) | 土曜日Sunday |
注釈Remarks
SET DATEFIRST の現在の設定を確認するには、@@DATEFIRST 関数を使います。To see the current setting of SET DATEFIRST, use the @@DATEFIRST function.
SET DATEFIRST の設定は、解析時ではなく実行時に設定されます。The setting of SET DATEFIRST is set at execute or run time and not at parse time.
SET DATEFIRST を指定しても DATEDIFF に影響はありません。Specifying SET DATEFIRST has no effect on DATEDIFF. DATEDIFF では、週の最初の曜日として常に日曜日を使用し、関数が確実に決定的になるようにします。DATEDIFF always uses Sunday as the first day of the week to ensure the function is deterministic.
アクセス許可Permissions
ロール public のメンバーシップが必要です。Requires membership in the public role.
例Examples
次の例では、日付値に対応する曜日を表示し、DATEFIRST
の設定を変更した場合の影響を示しています。The following example displays the day of the week for a date value and shows the effects of changing the DATEFIRST
setting.
-- 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