You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The specific part of the *date* argument for which `DATEPART` will return an **integer**. This table lists all valid *datepart* arguments.
51
52
@@ -82,17 +83,19 @@ An expression that resolves to one of the following data types:
82
83
83
84
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.
84
85
85
-
## Return Type
86
+
## Return Type
87
+
86
88
**int**
87
89
88
-
## Return Value
90
+
## Return Value
91
+
89
92
Each *datepart* and its abbreviations return the same value.
90
93
91
94
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.
92
95
93
96
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:
94
97
95
-
**.123456700**
98
+
**.123456700**
96
99
97
100
|*datepart*|Return value|
98
101
|---|---|
@@ -113,6 +116,7 @@ This table lists all *datepart* arguments, with corresponding return values, for
113
116
|**iso_week, isowk, isoww**|44|
114
117
115
118
## Week and weekday datepart arguments
119
+
116
120
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).
117
121
118
122
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
140
144
|6|17|1|
141
145
|7|16|7|
142
146
143
-
## year, month, and day datepart Arguments
147
+
## year, month, and day datepart Arguments
148
+
144
149
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.
145
150
146
-
## iso_week datepart
151
+
## iso_week datepart
152
+
147
153
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.
148
154
149
155
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
159
165
|Wednesday|1 January,<br /><br /> First Tuesday,<br /><br /> 1-7 days of year|Yes||
160
166
|Saturday|1 January,<br /><br /> First Friday,<br /><br /> 1-7 days of year|Yes||
161
167
162
-
## tzoffset
168
+
## tzoffset
169
+
163
170
`DATEPART` returns the **tzoffset** (**tz**) value as the number of minutes (signed). This statement returns a time zone offset of 310 minutes:
- For datetimeoffset and datetime2, tzoffset returns the time offset in minutes, where the offset for datetime2 is always 0 minutes.
170
179
- For data types that can implicitly convert to **datetimeoffset** or **datetime2**, `DATEPART` returns the time offset in minutes. Exception: other date / time data types.
171
180
- Parameters of all other types result in an error.
172
181
173
-
174
182
## smalldatetime date Argument
183
+
175
184
For a [smalldatetime](../../t-sql/data-types/smalldatetime-transact-sql.md)*date* value, `DATEPART` returns seconds as 00.
176
185
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
+
178
188
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*.
179
189
180
190
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);
195
205
```
196
206
197
207
## Fractional seconds
208
+
198
209
These statements show that `DATEPART` returns fractional seconds:
`DATEPART` can be used in the select list, WHERE, HAVING, GROUP BY, and ORDER BY clauses.
208
220
209
221
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.
210
222
211
-
## Examples
223
+
## Examples
224
+
212
225
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.
213
226
214
227
```sql
@@ -238,6 +251,6 @@ SELECT TOP(1) DATEPART (year,'12/20/1974') FROM dbo.DimCustomer;
238
251
```
239
252
240
253
## See also
254
+
241
255
[CAST and CONVERT (Transact-SQL)](../../t-sql/functions/cast-and-convert-transact-sql.md)
0 commit comments