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
Addresses valid concerns raised by public PR 8061. I did a full review of the wording and formatting in this article to make it clear when referring to data types, parameters, and dateparts.
Copy file name to clipboardExpand all lines: docs/t-sql/functions/datetrunc-transact-sql.md
+47-51Lines changed: 47 additions & 51 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ description: "Transact-SQL reference for the DATETRUNC function. This function r
4
4
author: aashnabafna-ms
5
5
ms.author: aashnabafna
6
6
ms.reviewer: derekw, maghan, randolphwest
7
-
ms.date: 07/26/2022
7
+
ms.date: 09/13/2022
8
8
ms.prod: sql
9
9
ms.technology: t-sql
10
10
ms.topic: reference
@@ -25,65 +25,61 @@ Starting with [!INCLUDE [sssql22-md](../../includes/sssql22-md.md)], this functi
25
25
## Syntax
26
26
27
27
```syntaxsql
28
-
DATETRUNC ( datepart, daterelatedvalue )
28
+
DATETRUNC ( datepart, date )
29
29
```
30
30
31
31
## Arguments
32
32
33
33
#### *datepart*
34
34
35
-
The datepart specifies the precision for truncation. This table lists all the valid datepart values for `DATETRUNC`, given that it's also a valid part of the input date type.
35
+
Specifies the precision for truncation. This table lists all the valid *datepart* values for `DATETRUNC`, given that it's also a valid part of the input date type.
36
36
37
-
|*datepart*|Abbreviations|Truncation Notes|
37
+
|*datepart*|Abbreviations|Truncation notes|
38
38
|---|---|---|
39
-
|**year**|**yy**, **yyyy**|
40
-
|**quarter**|**qq**, **q**|
41
-
|**month**|**mm**, **m**|
39
+
|**year**|**yy**, **yyyy**|
40
+
|**quarter**|**qq**, **q**|
41
+
|**month**|**mm**, **m**|
42
42
|**dayofyear**|**dy**, **y**|*dayofyear* is truncated in the same manner as *day*
43
43
|**day**|**dd**, **d**|*day* is truncated in the same manner as *dayofyear*
44
44
|**week**|**wk**, **ww**|Truncate to the first day of the week. In T-SQL, the first day of the week is defined by the `@@DATEFIRST` T-SQL setting. For a U.S. English environment, `@@DATEFIRST` defaults to 7 (Sunday).
45
45
|**iso\_week**|**isowk, isoww**|Truncate to the first day of an ISO Week. The first day of the week in the ISO8601 calendar system is Monday.
46
-
|**hour**|**hh**|
47
-
|**minute**|**mi, n**|
48
-
|**second**|**ss**, **s**|
49
-
|**millisecond**|**ms**|
46
+
|**hour**|**hh**|
47
+
|**minute**|**mi, n**|
48
+
|**second**|**ss**, **s**|
49
+
|**millisecond**|**ms**|
50
50
|**microsecond**|**mcs**|
51
51
52
52
> [!NOTE]
53
-
> The *weekday*, *timezoneoffset*, and *nanosecond* T-SQL dateparts are not supported for DATETRUNC.
53
+
> The *weekday*, *timezoneoffset*, and *nanosecond* T-SQL dateparts are not supported for `DATETRUNC`.
54
54
55
-
#### *daterelatedvalue*
55
+
#### *date*
56
56
57
-
The date-related value parameter accepts any expression, column, or user-defined variable that can resolve to any valid T-SQL date or time type. These are:
57
+
Accepts any expression, column, or user-defined variable that can resolve to any valid T-SQL date or time type. Valid types are:
58
58
59
59
-**smalldatetime**
60
-
61
60
-**datetime**
62
-
63
61
-**date**
64
-
65
62
-**time**
66
-
67
63
-**datetime2**
68
-
69
64
-**datetimeoffset**
70
65
71
-
> [!NOTE]
72
-
> DATETRUNC will also accept a string literal (of any string type) that can resolve to a **datetime2(7)**.
66
+
Don't confuse the *date* parameter with the **date** data type.
67
+
68
+
`DATETRUNC` will also accept a string literal (of any string type) that can resolve to a **datetime2(7)**.
73
69
74
70
## Return type
75
71
76
-
The returned data type for DATETRUNC is dynamic. DATETRUNC returns a truncated date of the same data type (and, if applicable, the same fractional time scale) as the input date. For example, if DATETRUNC was given a **datetimeoffset(3)** input date, it would return a **datetimeoffset(3)**. If it was given a string literal that could resolve to a **datetime2(7)**, DATETRUNC would return a **datetime2(7)**.
72
+
The returned data type for `DATETRUNC` is dynamic. `DATETRUNC` returns a truncated date of the same data type (and, if applicable, the same fractional time scale) as the input date. For example, if `DATETRUNC` was given a **datetimeoffset(3)** input date, it would return a **datetimeoffset(3)**. If it was given a string literal that could resolve to a **datetime2(7)**, `DATETRUNC` would return a **datetime2(7)**.
77
73
78
74
## Fractional time scale precision
79
75
80
-
Milliseconds have a fractional time scale of 3 (`.123`), microseconds have a fractional time scale of 6 (`.123456`), and nanoseconds have a fractional time scale of 9 (`.123456789`). The **time**, **datetime2**, and **datetimeoffset** data types allow a maximum fractional time scale of 7 (`.1234567`). Therefore, to truncate to the *millisecond*datepart, the fractional time scale must be at least 3. Similarly, to truncate to the *microsecond*datepart, the fractional time scale must be at least 6. DATETRUNC doesn't support the *nanosecond*datepart since no T-SQL date type supports a fractional time scale of 9.
76
+
Milliseconds have a fractional time scale of 3 (`.123`), microseconds have a fractional time scale of 6 (`.123456`), and nanoseconds have a fractional time scale of 9 (`.123456789`). The **time**, **datetime2**, and **datetimeoffset** data types allow a maximum fractional time scale of 7 (`.1234567`). Therefore, to truncate to the `millisecond`*datepart*, the fractional time scale must be at least 3. Similarly, to truncate to the `microsecond`*datepart*, the fractional time scale must be at least 6. `DATETRUNC` doesn't support the `nanosecond`*datepart* since no T-SQL date type supports a fractional time scale of 9.
81
77
82
78
## Examples
83
79
84
-
### A. Dateparts
80
+
### A. Use different *datepart* options
85
81
86
-
The following examples illustrate the use of the different dateparts:
82
+
The following examples illustrate the use of various *datepart* options:
The `TransactionDate` column from the `Sales.CustomerTransactions` table serves as an example *column* argument for the date-related value parameter:
183
+
The `TransactionDate` column from the `Sales.CustomerTransactions` table serves as an example *column* argument for the *date* parameter:
188
184
189
185
```sql
190
186
USE WideWorldImporters;
@@ -204,9 +200,9 @@ WHERE InvoiceID IS NOT NULL
204
200
AND DATETRUNC(month, TransactionDate) >='2015-12-01';
205
201
```
206
202
207
-
### F. Date-related expressions
203
+
### F. Expressions and the *date* parameter
208
204
209
-
The date-related value parameter accepts any expression that can resolve to a T-SQL date type or any string literal that can resolve to a **datetime2(7)**. The `TransactionDate` column from the `Sales.CustomerTransactions` table serves as an artificial argument to exemplify the use of an *expression* for the date-related value parameter:
205
+
The *date*parameter accepts any expression that can resolve to a T-SQL date type or any string literal that can resolve to a **datetime2(7)**. The `TransactionDate` column from the `Sales.CustomerTransactions` table serves as an artificial argument to exemplify the use of an *expression* for the *date* parameter:
210
206
211
207
```sql
212
208
SELECT DATETRUNC(m, SYSDATETIME());
@@ -220,41 +216,41 @@ FROM Sales.CustomerTransactions;
220
216
GO
221
217
```
222
218
223
-
### G. Truncate a date-related value to a datepart representing its maximum precision
219
+
### G. Truncate a *date*to a *datepart* representing its maximum precision
224
220
225
-
If the datepart has the same unit maximum precision as the input date type, truncating the input date to this datepart would have no effect.
221
+
If the *datepart* has the same unit maximum precision as the input date type, truncating the input date to this *datepart* would have no effect.
226
222
227
-
Example 1:
223
+
#### Example 1
228
224
229
225
```sql
230
226
DECLARE @d datetime ='2015-04-29 05:06:07.123';
231
227
SELECT'Input', @d;
232
228
SELECT'Truncated', DATETRUNC(millisecond, @d);
233
229
```
234
230
235
-
Here's the result set, which illustrates that the input datetime and the truncated datetime values are the same:
231
+
Here's the result set, which illustrates that the input **datetime** and the truncated *date* parameter are the same:
236
232
237
233
```output
238
234
Input 2015-04-29 05:06:07.123
239
235
Truncated 2015-04-29 05:06:07.123
240
236
```
241
237
242
-
Example 2:
238
+
#### Example 2
243
239
244
240
```sql
245
241
DECLARE @d date='2050-04-04';
246
242
SELECT'Input', @d;
247
243
SELECT'Truncated', DATETRUNC(day, @d);
248
244
```
249
245
250
-
Here's the result set, which illustrates that the input date and the truncated date are the same:
246
+
Here's the result set, which illustrates that the input **datetime** and the truncated *date* parameter are the same:
251
247
252
248
```output
253
249
Input 2050-04-04
254
250
Truncated 2050-04-04
255
251
```
256
252
257
-
Example 3: **smalldatetime** precision
253
+
#### Example 3: smalldatetime precision
258
254
259
255
**smalldatetime** is only precise up to the nearest minute, even though it has a field for seconds. Therefore, truncating it to the nearest minute or the nearest second would have no effect.
260
256
@@ -265,15 +261,15 @@ SELECT 'Truncated to minute', DATETRUNC(minute, @d)
265
261
SELECT'Truncated to second', DATETRUNC(second, @d);
266
262
```
267
263
268
-
The result set illustrates that the input smalldatetime value is the same as both the truncated values:
264
+
The result set illustrates that the input **smalldatetime** value is the same as both the truncated values:
269
265
270
266
```output
271
267
Input 2009-09-11 12:42:00
272
268
Truncated to minute 2009-09-11 12:42:00
273
269
Truncated to second 2009-09-11 12:42:00
274
270
```
275
271
276
-
Example 4: **datetime** precision
272
+
#### Example 4: datetime precision
277
273
278
274
**datetime** is only precise up to 3.33 milliseconds. Therefore, truncating a **datetime** to a millisecond may yield results that are different than what the user expects. However, this truncated value is the same as the internally stored **datetime** value.
279
275
@@ -283,7 +279,7 @@ SELECT 'Input', @d;
283
279
SELECT'Truncated', DATETRUNC(millisecond, @d);
284
280
```
285
281
286
-
Here's the result set, which illustrates that the truncated date is the same as the stored date. Even if this is different than what the user may have expected based on the DECLARE statement.
282
+
Here's the result set, which illustrates that the truncated *date* is the same as the stored *date*. This may be different what you expect based on the `DECLARE` statement.
A `DATE TOO SMALL` error is thrown if the date truncation attempts to backtrack to a date before the minimum date supported by that date type. This only occurs when using the *week*datepart. It can't occur when using the *iso_week*datepart since all the T-SQL date types coincidentally use a Monday for their minimum dates. Here's an example with the corresponding result error message:
291
+
A `DATE TOO SMALL` error is thrown if the *date* truncation attempts to backtrack to a date before the minimum date supported by that data type. This only occurs when using the `week`*datepart*. It can't occur when using the `iso_week`*datepart*, since all the T-SQL date types coincidentally use a Monday for their minimum dates. Here's an example with the corresponding result error message:
296
292
297
293
```sql
298
294
DECLARE @d date='0001-01-01 00:00:00';
@@ -304,11 +300,11 @@ Msg 9837, Level 16, State 3, Line 84
304
300
An invalid date value was encountered: The date value is less than the minimum date value allowed for the data type.
305
301
```
306
302
307
-
A `DATEPART` error is thrown if the datepart used isn't supported by the DATETRUNC function or the input date data type. This can occur when:
303
+
A `DATEPART` error is thrown if the *datepart* used isn't supported by the `DATETRUNC` function or the input date data type. This can occur when:
308
304
309
-
1. A datepart not supported by DATETRUNC is used (namely, *weekday*, *tzoffset*, or *nanosecond*)
305
+
1. A *datepart* not supported by `DATETRUNC` is used (namely, `weekday`, `tzoffset`, or `nanosecond`)
310
306
311
-
2. A *time*-related datepart is used with the *date* data type or a *date*-related datepart is used with the *time* data type. Here's an example with the corresponding result error message:
307
+
1. A **time**-related *datepart* is used with the **date** data type or a **date**-related *datepart* is used with the **time** data type. Here's an example with the corresponding result error message:
312
308
313
309
```sql
314
310
DECLARE @d time='12:12:12.1234567';
@@ -320,7 +316,7 @@ A `DATEPART` error is thrown if the datepart used isn't supported by the DATETRU
320
316
The datepart year is not supported by date function datetrunc for data type time.
321
317
```
322
318
323
-
3. The datepart requires a higher fractional time scale precision than what is supported by the data type (See section onFractional Time Scale Precision). Here's an example with the corresponding result error message:
319
+
1. The *datepart* requires a higher fractional time scale precision than what is supported by the data type (See [Fractional time scale precision](#fractional-time-scale-precision)). Here's an example with the corresponding result error message:
0 commit comments