Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit b92e4cf

Browse files
committed
DATETRUNC: Consolidate public PR and review
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.
1 parent 3b9a5ca commit b92e4cf

1 file changed

Lines changed: 47 additions & 51 deletions

File tree

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

Lines changed: 47 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: "Transact-SQL reference for the DATETRUNC function. This function r
44
author: aashnabafna-ms
55
ms.author: aashnabafna
66
ms.reviewer: derekw, maghan, randolphwest
7-
ms.date: 07/26/2022
7+
ms.date: 09/13/2022
88
ms.prod: sql
99
ms.technology: t-sql
1010
ms.topic: reference
@@ -25,65 +25,61 @@ Starting with [!INCLUDE [sssql22-md](../../includes/sssql22-md.md)], this functi
2525
## Syntax
2626

2727
```syntaxsql
28-
DATETRUNC ( datepart, daterelatedvalue )
28+
DATETRUNC ( datepart, date )
2929
```
3030

3131
## Arguments
3232

3333
#### *datepart*
3434

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.
3636

37-
|*datepart*|Abbreviations|Truncation Notes|
37+
|*datepart*|Abbreviations|Truncation notes|
3838
|---|---|---|
39-
|**year**|**yy**, **yyyy**|
40-
|**quarter**|**qq**, **q**|
41-
|**month**|**mm**, **m**|
39+
|**year**|**yy**, **yyyy**|
40+
|**quarter**|**qq**, **q**|
41+
|**month**|**mm**, **m**|
4242
|**dayofyear**|**dy**, **y**|*dayofyear* is truncated in the same manner as *day*
4343
|**day**|**dd**, **d**|*day* is truncated in the same manner as *dayofyear*
4444
|**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).
4545
|**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**|
5050
|**microsecond**|**mcs**|
5151

5252
> [!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`.
5454
55-
#### *daterelatedvalue*
55+
#### *date*
5656

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:
5858

5959
- **smalldatetime**
60-
6160
- **datetime**
62-
6361
- **date**
64-
6562
- **time**
66-
6763
- **datetime2**
68-
6964
- **datetimeoffset**
7065

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)**.
7369

7470
## Return type
7571

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)**.
7773

7874
## Fractional time scale precision
7975

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.
8177

8278
## Examples
8379

84-
### A. Dateparts
80+
### A. Use different *datepart* options
8581

86-
The following examples illustrate the use of the different dateparts:
82+
The following examples illustrate the use of various *datepart* options:
8783

8884
```sql
8985
DECLARE @d datetime2 = '2021-12-08 11:30:15.1234567';
@@ -120,7 +116,7 @@ Microsecond 2021-12-08 11:30:15.1234560
120116

121117
### B. @@DATEFIRST setting
122118

123-
The following examples illustrate the use of the `@@DATEFIRST` setting with the *week* datepart:
119+
The following examples illustrate the use of the `@@DATEFIRST` setting with the `week` *datepart*:
124120

125121
```sql
126122
DECLARE @d datetime2 = '2021-11-11 11:11:11.1234567';
@@ -144,7 +140,7 @@ Week-3 2021-11-10 00:00:00.0000000
144140

145141
### C. Date literals
146142

147-
The following examples illustrate the use of date-related `literals`:
143+
The following examples illustrate the use of *date* parameter literals:
148144

149145
```sql
150146
SELECT DATETRUNC(month, '1998-03-04');
@@ -167,9 +163,9 @@ Here's the result set (all the results are of type **datetime2(7)**):
167163
1998-03-04 10:10:00.0000000
168164
```
169165

170-
### D. Date-related variables
166+
### D. Variables and the *date* parameter
171167

172-
The following example illustrates the use of a date-related `variable`:
168+
The following example illustrates the use of the *date* parameter:
173169

174170
```sql
175171
DECLARE @d datetime2 = '1998-12-11 02:03:04.1234567';
@@ -182,9 +178,9 @@ Here's the result:
182178
1998-12-11 00:00:00.0000000
183179
```
184180

185-
### E. Date columns
181+
### E. Columns and the *date* parameter
186182

187-
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:
188184

189185
```sql
190186
USE WideWorldImporters;
@@ -204,9 +200,9 @@ WHERE InvoiceID IS NOT NULL
204200
AND DATETRUNC(month, TransactionDate) >= '2015-12-01';
205201
```
206202

207-
### F. Date-related expressions
203+
### F. Expressions and the *date* parameter
208204

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:
210206

211207
```sql
212208
SELECT DATETRUNC(m, SYSDATETIME());
@@ -220,41 +216,41 @@ FROM Sales.CustomerTransactions;
220216
GO
221217
```
222218

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
224220

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.
226222

227-
Example 1:
223+
#### Example 1
228224

229225
```sql
230226
DECLARE @d datetime = '2015-04-29 05:06:07.123';
231227
SELECT 'Input', @d;
232228
SELECT 'Truncated', DATETRUNC(millisecond, @d);
233229
```
234230

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:
236232

237233
```output
238234
Input 2015-04-29 05:06:07.123
239235
Truncated 2015-04-29 05:06:07.123
240236
```
241237

242-
Example 2:
238+
#### Example 2
243239

244240
```sql
245241
DECLARE @d date = '2050-04-04';
246242
SELECT 'Input', @d;
247243
SELECT 'Truncated', DATETRUNC(day, @d);
248244
```
249245

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:
251247

252248
```output
253249
Input 2050-04-04
254250
Truncated 2050-04-04
255251
```
256252

257-
Example 3: **smalldatetime** precision
253+
#### Example 3: smalldatetime precision
258254

259255
**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.
260256

@@ -265,15 +261,15 @@ SELECT 'Truncated to minute', DATETRUNC(minute, @d)
265261
SELECT 'Truncated to second', DATETRUNC(second, @d);
266262
```
267263

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:
269265

270266
```output
271267
Input 2009-09-11 12:42:00
272268
Truncated to minute 2009-09-11 12:42:00
273269
Truncated to second 2009-09-11 12:42:00
274270
```
275271

276-
Example 4: **datetime** precision
272+
#### Example 4: datetime precision
277273

278274
**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.
279275

@@ -283,7 +279,7 @@ SELECT 'Input', @d;
283279
SELECT 'Truncated', DATETRUNC(millisecond, @d);
284280
```
285281

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.
287283

288284
```output
289285
Input 2020-02-02 02:02:02.003
@@ -292,7 +288,7 @@ Truncated 2020-02-02 02:02:02.003
292288

293289
## Remarks
294290

295-
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:
296292

297293
```sql
298294
DECLARE @d date= '0001-01-01 00:00:00';
@@ -304,11 +300,11 @@ Msg 9837, Level 16, State 3, Line 84
304300
An invalid date value was encountered: The date value is less than the minimum date value allowed for the data type.
305301
```
306302

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:
308304

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`)
310306

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:
312308

313309
```sql
314310
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
320316
The datepart year is not supported by date function datetrunc for data type time.
321317
```
322318

323-
3. The datepart requires a higher fractional time scale precision than what is supported by the data type (See section on Fractional 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:
324320

325321
```sql
326322
DECLARE @d datetime2(3) = '2021-12-12 12:12:12.12345';
@@ -334,6 +330,6 @@ A `DATEPART` error is thrown if the datepart used isn't supported by the DATETRU
334330

335331
## See also
336332

337-
- [@@DATEFIRST](../../t-sql/functions/datefirst-transact-sql.md)
338-
- [DATEPART](../../t-sql/functions/datepart-transact-sql.md)
339-
- [Valid date and time types supported by TSQL](date-and-time-data-types-and-functions-transact-sql.md)
333+
- [@@DATEFIRST (Transact-SQL)](datefirst-transact-sql.md)
334+
- [DATEPART (Transact-SQL)](datepart-transact-sql.md)
335+
- [Date and time data types and functions (Transact-SQL)](date-and-time-data-types-and-functions-transact-sql.md)

0 commit comments

Comments
 (0)