Skip to content

Commit ebe2fd5

Browse files
committed
[SQL Server 2022] DATETRUNC CTP 2.1 new article
1 parent b2316a7 commit ebe2fd5

3 files changed

Lines changed: 320 additions & 19 deletions

File tree

docs/t-sql/functions/datepart-transact-sql.md

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
---
22
title: "DATEPART (Transact-SQL)"
33
description: "Transact-SQL reference for the DATEPART function. This function returns an integer corresponding to the datepart of a specified date."
4-
author: markingmyname
5-
ms.author: maghan
6-
ms.reviewer: ""
7-
ms.date: "07/29/2017"
84
ms.prod: sql
9-
ms.prod_service: "database-engine, sql-database, synapse-analytics, pdw"
105
ms.technology: t-sql
116
ms.topic: reference
127
f1_keywords:
@@ -25,11 +20,16 @@ helpviewer_keywords:
2520
- "dates [SQL Server], dateparts"
2621
dev_langs:
2722
- "TSQL"
23+
author: markingmyname
24+
ms.author: maghan
25+
ms.reviewer: ""
26+
ms.date: "07/29/2017"
2827
monikerRange: ">= aps-pdw-2016 || = azuresqldb-current || = azure-sqldw-latest || >= sql-server-2016 || >= sql-server-linux-2017 || = azuresqldb-mi-current"
2928
---
29+
3030
# DATEPART (Transact-SQL)
31-
[!INCLUDE [sql-asdb-asdbmi-asa-pdw](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw.md)]
3231

32+
[!INCLUDE [sql-asdb-asdbmi-asa-pdw](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw.md)]
3333

3434
This function returns an integer representing the specified *datepart* of the specified *date*.
3535

@@ -46,6 +46,7 @@ DATEPART ( datepart , date )
4646
[!INCLUDE[sql-server-tsql-previous-offline-documentation](../../includes/sql-server-tsql-previous-offline-documentation.md)]
4747

4848
## Arguments
49+
4950
*datepart*
5051
The specific part of the *date* argument for which `DATEPART` will return an **integer**. This table lists all valid *datepart* arguments.
5152

@@ -82,17 +83,19 @@ An expression that resolves to one of the following data types:
8283

8384
For *date*, `DATEPART` will accept a column expression, expression, string literal, or user-defined variable. Use four-digit years to avoid ambiguity issues. See [Configure the two digit year cutoff Server Configuration Option](../../database-engine/configure-windows/configure-the-two-digit-year-cutoff-server-configuration-option.md) for information about two-digit years.
8485

85-
## Return Type
86+
## Return Type
87+
8688
**int**
8789

88-
## Return Value
90+
## Return Value
91+
8992
Each *datepart* and its abbreviations return the same value.
9093

9194
The return value depends on the language environment set by using [SET LANGUAGE](../../t-sql/statements/set-language-transact-sql.md), and by the [Configure the default language Server Configuration Option](../../database-engine/configure-windows/configure-the-default-language-server-configuration-option.md) of the login. The return value depends on [SET DATEFORMAT](../../t-sql/statements/set-dateformat-transact-sql.md) if *date* is a string literal of some formats. SET DATEFORMAT does not change the return value when the date is a column expression of a date or time data type.
9295

9396
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')`. The *date* argument has a **datetimeoffset(7)** data type. The last two positions of the **nanosecond** *datepart* return value are always `00` and this value has a scale of 9:
9497

95-
**.123456700**
98+
**.123456700**
9699

97100
|*datepart*|Return value|
98101
|---|---|
@@ -113,6 +116,7 @@ This table lists all *datepart* arguments, with corresponding return values, for
113116
|**iso_week, isowk, isoww**|44|
114117

115118
## Week and weekday datepart arguments
119+
116120
For a **week** (**wk**, **ww**) or **weekday** (**dw**) *datepart*, the `DATEPART` return value depends on the value set by [SET DATEFIRST](../../t-sql/statements/set-datefirst-transact-sql.md).
117121

118122
January 1 of any year defines the starting number for the **week** _datepart_. For example:
@@ -140,10 +144,12 @@ serves as the default. After setting DATEFIRST, use this suggested SQL statement
140144
|6|17|1|
141145
|7|16|7|
142146

143-
## year, month, and day datepart Arguments
147+
## year, month, and day datepart Arguments
148+
144149
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](../../t-sql/functions/year-transact-sql.md), [MONTH](../../t-sql/functions/month-transact-sql.md), and [DAY](../../t-sql/functions/day-transact-sql.md), respectively.
145150

146-
## iso_week datepart
151+
## iso_week datepart
152+
147153
ISO 8601 includes the ISO week-date system, a numbering system for weeks. Each week is associated with the year in which Thursday occurs. 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.
148154

149155
Note: the highest week number in a year could be either 52 or 53.
@@ -159,22 +165,26 @@ The numbering systems of different countries/regions might not comply with the I
159165
|Wednesday|1 January,<br /><br /> First Tuesday,<br /><br /> 1-7 days of year|Yes||
160166
|Saturday|1 January,<br /><br /> First Friday,<br /><br /> 1-7 days of year|Yes||
161167

162-
## tzoffset
168+
## tzoffset
169+
163170
`DATEPART` returns the **tzoffset** (**tz**) value as the number of minutes (signed). This statement returns a time zone offset of 310 minutes:
164171

165172
```sql
166173
SELECT DATEPART (tzoffset, '2007-05-10 00:00:01.1234567 +05:10');
167-
```
174+
```
175+
168176
`DATEPART` renders the tzoffset value as follows:
177+
169178
- For datetimeoffset and datetime2, tzoffset returns the time offset in minutes, where the offset for datetime2 is always 0 minutes.
170179
- For data types that can implicitly convert to **datetimeoffset** or **datetime2**, `DATEPART` returns the time offset in minutes. Exception: other date / time data types.
171180
- Parameters of all other types result in an error.
172181

173-
174182
## smalldatetime date Argument
183+
175184
For a [smalldatetime](../../t-sql/data-types/smalldatetime-transact-sql.md) *date* value, `DATEPART` returns seconds as 00.
176185

177-
## Default Returned for a datepart That Is Not in a date Argument
186+
## Default Returned for a datepart That Is Not in a date Argument
187+
178188
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*.
179189

180190
For example, the default year-month-day for any **date** data type is 1900-01-01. This statement has date part arguments for *datepart*, a time argument for *date*, and it returns `1900, 1, 1, 1, 2`.
@@ -195,6 +205,7 @@ SELECT DATEPART(year, @t);
195205
```
196206

197207
## Fractional seconds
208+
198209
These statements show that `DATEPART` returns fractional seconds:
199210

200211
```sql
@@ -203,12 +214,14 @@ SELECT DATEPART(microsecond, '00:00:01.1234567'); -- Returns 123456
203214
SELECT DATEPART(nanosecond, '00:00:01.1234567'); -- Returns 123456700
204215
```
205216

206-
## Remarks
217+
## Remarks
218+
207219
`DATEPART` can be used in the select list, WHERE, HAVING, GROUP BY, and ORDER BY clauses.
208220

209221
DATEPART implicitly casts string literals as a **datetime2** type in [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)]. This means that DATENAME does not support the format YDM when the date is passed as a string. You must explicitly cast the string to a **datetime** or **smalldatetime** type to use the YDM format.
210222

211-
## Examples
223+
## Examples
224+
212225
This example returns the base year. The base year helps with date calculations. In the example, a number specifies the date. Notice that [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] interprets 0 as January 1, 1900.
213226

214227
```sql
@@ -238,6 +251,6 @@ SELECT TOP(1) DATEPART (year,'12/20/1974') FROM dbo.DimCustomer;
238251
```
239252

240253
## See also
254+
241255
[CAST and CONVERT &#40;Transact-SQL&#41;](../../t-sql/functions/cast-and-convert-transact-sql.md)
242-
243-
256+
[DATETRUNC](../../t-sql/functions/datetrunc-transact-sql.md)

0 commit comments

Comments
 (0)