DATEPART (Transact-SQL)DATEPART (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
この関数は、指定された date の指定された datepart を表す整数を返します。This function returns an integer representing the specified datepart of the specified date.
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 構文表記規則
Transact-SQL Syntax Conventions
構文Syntax
DATEPART ( datepart , date )
注意
SQL Server 2014 以前の Transact-SQL 構文を確認するには、以前のバージョンのドキュメントを参照してください。To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.
引数Arguments
datepartdatepart
DATEPART
によって 整数 が返される date 引数の特定の部分。The specific part of the date argument for which DATEPART
will return an integer. この表には、有効な datepart 引数をすべて一覧表示しています。This table lists all valid datepart arguments.
注意
DATEPART
は、datepart 引数に関して、ユーザー定義変数に相当するものは受け入れられません。DATEPART
does not accept user-defined variable equivalents for the datepart arguments.
datepartdatepart | 省略形Abbreviations |
---|---|
yearyear | yy、yyyyyy, yyyy |
quarterquarter | qq、qqq, q |
monthmonth | mm、mmm, m |
dayofyeardayofyear | dy、ydy, y |
dayday | dd、ddd, d |
weekweek | wk、wwwk, ww |
weekdayweekday | dwdw |
hourhour | hhhh |
minuteminute | mi、nmi, n |
secondsecond | ss、sss, s |
millisecondmillisecond | msms |
microsecondmicrosecond | mcsmcs |
nanosecondnanosecond | nsns |
tzoffsettzoffset | tztz |
iso_weekiso_week | isowk、isowwisowk, isoww |
datedate
次のいずれかのデータ型に解決される式。An expression that resolves to one of the following data types:
- datedate
- datetimedatetime
- datetimeoffsetdatetimeoffset
- datetime2datetime2
- smalldatetimesmalldatetime
- timetime
date の場合、DATEPART
では、列式、式、文字列リテラル、ユーザー定義の変数が受け入れられます。For date, DATEPART
will accept a column expression, expression, string literal, or user-defined variable. あいまいさの問題を排除するために、4 桁の西暦を使用してください。Use four-digit years to avoid ambiguity issues. 2 桁の西暦については、「two digit year cutoff サーバー構成オプションの構成」を参照してください。See Configure the two digit year cutoff Server Configuration Option for information about two-digit years.
戻り値の型Return Type
intint
戻り値Return Value
-各日付構成要素とその省略形は、同じ値を返します。Each datepart and its abbreviations return the same value.
戻り値は、SET LANGUAGE と、ログインの default language サーバー構成オプションの構成で設定した言語環境に依存します。The return value depends on the language environment set by using SET LANGUAGE, and by the Configure the default language Server Configuration Option of the login. date がなんらかの形式の文字列リテラルである場合、戻り値は SET DATEFORMAT に依存します。The return value depends on SET DATEFORMAT if date is a string literal of some formats. date が日付データ型や時刻データ型の列式である場合、SET DATEFORMAT によって戻り値が変わることはありません。SET DATEFORMAT does not change the return value when the date is a column expression of a date or time data type.
この表は、SELECT DATEPART(datepart,'2007-10-30 12:15:32.1234567 +05:10')
ステートメントについて、すべての datepart 引数と、対応する戻り値を一覧にしたものです。This table lists all datepart arguments, with corresponding return values, for the statement SELECT DATEPART(datepart,'2007-10-30 12:15:32.1234567 +05:10')
. date 引数のデータ型は datetimeoffset(7) です。The date argument has a datetimeoffset(7) data type. nanosecond datepart の戻り値の末尾 2 桁は常に 00
であり、この値の小数点以下桁数は 9 桁です。The last two positions of the nanosecond datepart return value are always 00
and this value has a scale of 9:
.123456700.123456700
datepartdatepart | 戻り値Return value |
---|---|
year、yyyy、yyyear, yyyy, yy | 20072007 |
quarter、qq、qquarter, qq, q | 44 |
month、mm、mmonth, mm, m | 1010 |
dayofyear、dy、ydayofyear, dy, y | 303303 |
day、dd、dday, dd, d | 3030 |
week、wk、wwweek, wk, ww | 4444 |
weekday、dwweekday, dw | 33 |
hour、hhhour, hh | 1212 |
minute、nminute, n | 1515 |
second、ss、ssecond, ss, s | 3232 |
millisecond、msmillisecond, ms | 123123 |
microsecond、mcsmicrosecond, mcs | 123456123456 |
nanosecond、nsnanosecond, ns | 123456700123456700 |
tzoffset, tztzoffset, tz | 310310 |
iso_week, isowk, isowwiso_week, isowk, isoww | 4444 |
week および weekday (datepart 引数)Week and weekday datepart arguments
week (wk、ww) または weekday (dw) datepart の場合、DATEPART
の戻り値は、SET DATEFIRST で設定された値によって変わります。For a week (wk, ww) or weekday (dw) datepart, the DATEPART
return value depends on the value set by SET DATEFIRST.
任意の年の 1 月 1 日が、week datepart の開始番号と定義されます。January 1 of any year defines the starting number for the week datepart. 次に例を示します。For example:
DATEPART (wk, 'Jan 1, xxx x') = 1DATEPART (wk, 'Jan 1, xxx x') = 1
この xxxx は任意の年です。where xxxx is any year.
この表は、week と weekday の datepart の戻り値を示しています。SET DATEFIRST 引数には、それぞれ '2007-04-21' が指定されています。This table shows the return value for the week and weekday datepart for '2007-04-21 ' for each SET DATEFIRST argument. 2007 年 1 月 1 日は月曜日です。January 1, 2007 falls on a Monday. 2007 年 4 月 21 日は土曜日です。April 21, 2007 falls on a Saturday. 米国英語の場合、For U.S. English,
SET DATEFIRST 7 -- ( Sunday )
が既定として使用されます。serves as the default. DATEFIRST を設定した後、datepart テーブル値に次の推奨される SQL ステートメントを使用します。After setting DATEFIRST, use this suggested SQL statement for the datepart table values:
SELECT DATEPART(week, '2007-04-21 '), DATEPART(weekday, '2007-04-21 ')
SET DATEFIRSTSET DATEFIRST 引数argument |
weekweek 返される値returned |
weekdayweekday 返される値returned |
---|---|---|
11 | 1616 | 66 |
22 | 1717 | 55 |
33 | 1717 | 44 |
44 | 1717 | 33 |
55 | 1717 | 22 |
66 | 1717 | 11 |
77 | 1616 | 77 |
year、month、day (datepart 引数)year, month, and day datepart Arguments
DATEPART (year, date)、DATEPART (month, date)、DATEPART (day, date) で返される値は、それぞれ YEAR、MONTH、DAY の各関数で返される値と同じです。The values that are returned for DATEPART (year, date), DATEPART (month, date), and DATEPART (day, date) are the same as those returned by the functions YEAR, MONTH, and DAY, respectively.
iso_week datepartiso_week datepart
ISO 8601 には、ISO 週日付方式 (週番号方式) が規定されています。ISO 8601 includes the ISO week-date system, a numbering system for weeks. それぞれの週は、木曜日が出現する年と関連付けられます。Each week is associated with the year in which Thursday occurs. たとえば、2004 年の第 1 週 (2004W01) は、2003 年 12 月 29 日月曜日から 2004 年 1 月 4 日 日曜日です。For example, week 1 of 2004 (2004W01) covered Monday, 29 December 2003 to Sunday, 4 January 2004. ヨーロッパの国/地域では、通常、このスタイルの付番方式が使用されます。European countries / regions typically use this style of numbering. 通常、ヨーロッパ以外の国/地域はこの方式を使用しません。Non-European countries / regions typically do not use it.
注: 1 年の最大週番号は 52 または 53 のいずれかになります。Note: the highest week number in a year could be either 52 or 53.
異なる国や地域では、付番方式が ISO 標準に準拠していない可能性があります。The numbering systems of different countries/regions might not comply with the ISO standard. この表は 6 つの可能性を示しています。This table shows six possibilities:
週の最初の曜日First day of week | 年の最初の週の構成First week of year contains | 2 回割り当てられる週の有無Weeks assigned two times | 利用されている地域Used by/in |
---|---|---|---|
土曜日Sunday | 1 月 1 日1 January, 最初の土曜日First Saturday, 年の 1 から 7 日間1-7 days of year |
はいYes | United StatesUnited States |
月曜日Monday | 1 月 1 日1 January, 最初の日曜日First Sunday, 年の 1 から 7 日間1-7 days of year |
はいYes | 欧州およびイギリスMost of Europe and the United Kingdom |
月曜日Monday | 1 月 4 日4 January, 最初の木曜日First Thursday, 年の 4 から 7 日間4-7 days of year |
いいえNo | ISO 8601、ノルウェー、およびスウェーデンISO 8601, Norway, and Sweden |
月曜日Monday | 1 月 7 日7 January, 最初の月曜日First Monday, 年の 7 日間7 days of year |
いいえNo | |
水曜日Wednesday | 1 月 1 日1 January, 最初の火曜日First Tuesday, 年の 1 から 7 日間1-7 days of year |
はいYes | |
土曜日Saturday | 1 月 1 日1 January, 最初の金曜日First Friday, 年の 1 から 7 日間1-7 days of year |
はいYes |
tzoffsettzoffset
DATEPART
は、符号付きの分数として tzoffset (tz) 値を返します。DATEPART
returns the tzoffset (tz) value as the number of minutes (signed). 次のステートメントは、310 分のタイム ゾーン オフセットを返します。This statement returns a time zone offset of 310 minutes:
SELECT DATEPART (tzoffset, '2007-05-10 00:00:01.1234567 +05:10');
DATEPART
では、tzoffset 値が次のようにレンダリングされます。DATEPART
renders the tzoffset value as follows:
- datetimeoffset と datetime2 の場合は、tzoffset は分単位で時刻オフセットを返します。datetime2 のオフセットは常に 0 分です。For datetimeoffset and datetime2, tzoffset returns the time offset in minutes, where the offset for datetime2 is always 0 minutes.
- 暗黙的に datetimeoffset または datetime2 に変換できるデータ型の場合、
DATEPART
は時刻オフセットを分単位で返します。For data types that can implicitly convert to datetimeoffset or datetime2,DATEPART
returns the time offset in minutes. 例外: 他の日付/時刻データ型。Exception: other date / time data types. - 他のすべての型のパラメーターは、エラーが発生します。Parameters of all other types result in an error.
smalldatetime (date 引数)smalldatetime date Argument
smalldatetime date 値の場合、DATEPART
は秒を 00 として返します。For a smalldatetime date value, DATEPART
returns seconds as 00.
date 引数に存在しない datepart を指定した場合に返される既定値Default Returned for a datepart That Is Not in a date Argument
date 引数のデータ型に指定した datepart がない場合、リテラルが date に指定されている場合にのみ、DATEPART
はその datepart の既定値を返します。If the date argument data type does not have the specified datepart, DATEPART
will return the default for that datepart only when a literal is specified for date.
など、既定の年-月-日の任意の 日付 データ型は 1900年-01-01 です。For example, the default year-month-day for any date data type is 1900-01-01. このステートメントでは、datepart 引数と date 引数にそれぞれ日付部分と時刻を表す値が指定されています。このステートメントは 1900, 1, 1, 1, 2
を返します。This statement has date part arguments for datepart, a time argument for date, and it returns 1900, 1, 1, 1, 2
.
SELECT DATEPART(year, '12:10:30.123')
,DATEPART(month, '12:10:30.123')
,DATEPART(day, '12:10:30.123')
,DATEPART(dayofyear, '12:10:30.123')
,DATEPART(weekday, '12:10:30.123');
date が変数またはテーブル列として指定され、その変数または列のデータ型に datepart が指定されていない場合、DATEPART
はエラー 9810 を返します。If date is specified as a variable or table column, and the data type for that variable or column does not have the specified datepart, DATEPART
will return error 9810. この例では、変数 @t は time データ型です。In this example, variable @t has a time data type. time データ型の日付部分の年度が無効なため、この例は失敗します。The example fails because the date part year is invalid for the time data type:
DECLARE @t time = '12:10:30.123';
SELECT DATEPART(year, @t);
秒の小数部Fractional seconds
これらのステートメントは、DATEPART
が秒の小数部を返すことを示しています。These statements show that DATEPART
returns fractional seconds:
SELECT DATEPART(millisecond, '00:00:01.1234567'); -- Returns 123
SELECT DATEPART(microsecond, '00:00:01.1234567'); -- Returns 123456
SELECT DATEPART(nanosecond, '00:00:01.1234567'); -- Returns 123456700
解説Remarks
DATEPART
は、選択リスト、WHERE、HAVING、GROUP BY、および ORDER BY 句で使用できます。DATEPART
can be used in the select list, WHERE, HAVING, GROUP BY, and ORDER BY clauses.
SQL Server 2019 (15.x)SQL Server 2019 (15.x) では、DATEPART は文字列リテラルを datetime2 型として暗黙的にキャストします。DATEPART implicitly casts string literals as a datetime2 type in SQL Server 2019 (15.x)SQL Server 2019 (15.x). つまり、DATENAME では、日付が文字列として渡される場合、YDM 形式がサポートされません。This means that DATENAME does not support the format YDM when the date is passed as a string. 文字列を明示的にキャストする必要があります、 datetime または smalldatetime YDM 形式を使用する型。You must explicitly cast the string to a datetime or smalldatetime type to use the YDM format.
例Examples
この例では、基準年を返します。This example returns the base year. この基準年は、日付の計算に役立ちます。The base year helps with date calculations. この例では、数値で日付を指定します。In the example, a number specifies the date. SQL ServerSQL Server は、0 を 1900 年 1 月 1 日と解釈することに注意してください。Notice that SQL ServerSQL Server interprets 0 as January 1, 1900.
SELECT DATEPART(year, 0), DATEPART(month, 0), DATEPART(day, 0);
-- Returns: 1900 1 1
この例は、日付 12/20/1974
の日の部分を返します。This example returns the day part of the date 12/20/1974
.
-- Uses AdventureWorks
SELECT TOP(1) DATEPART (day,'12/20/1974') FROM dbo.DimCustomer;
-- Returns: 20
この例は、日付 12/20/1974
の年の部分を返します。This example returns the year part of the date 12/20/1974
.
-- Uses AdventureWorks
SELECT TOP(1) DATEPART (year,'12/20/1974') FROM dbo.DimCustomer;
-- Returns: 1974
関連項目See also
CAST および CONVERT (Transact-SQL)CAST and CONVERT (Transact-SQL)