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
Copy file name to clipboardExpand all lines: docs/t-sql/functions/dateadd-transact-sql.md
+8-145Lines changed: 8 additions & 145 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "DATEADD (Transact-SQL) | Microsoft Docs"
3
3
ms.custom: ""
4
-
ms.date: "03/14/2017"
4
+
ms.date: "06/29/2017"
5
5
ms.prod: "sql-non-specified"
6
6
ms.reviewer: ""
7
7
ms.suite: ""
@@ -42,8 +42,6 @@ manager: "jhubbard"
42
42
## Syntax
43
43
44
44
```
45
-
-- Syntax for SQL Server, Azure SQL Database, Azure SQL Data Warehouse, Parallel Data Warehouse
46
-
47
45
DATEADD (datepart , number , date )
48
46
```
49
47
@@ -89,9 +87,10 @@ DATEADD (datepart , number , date )
89
87
90
88
If *datepart* is **month** and the *date* month has more days than the return month and the *date* day does not exist in the return month, the last day of the return month is returned. For example, September has 30 days; therefore, the two following statements return 2006-09-30 00:00:00.000:
91
89
92
-
`SELECT DATEADD(month, 1, '2006-08-30');`
93
-
94
-
`SELECT DATEADD(month, 1, '2006-08-31');`
90
+
```
91
+
SELECT DATEADD(month, 1, '2006-08-30');
92
+
SELECT DATEADD(month, 1, '2006-08-31');
93
+
```
95
94
96
95
## number Argument
97
96
The *number* argument cannot exceed the range of **int**. In the following statements, the argument for *number* exceeds the range of **int** by 1. The following error message is returned: "`Msg 8115, Level 16, State 2, Line 1. Arithmetic overflow error converting expression to data type int."`
The seconds part of a [smalldatetime](../../t-sql/data-types/smalldatetime-transact-sql.md) value is always 00. If *date* is **smalldatetime**, the following apply:
114
113
115
114
- If *datepart* is **second** and *number* is between -30 and +29, no addition is performed.
116
-
117
115
- If *datepart* is **second** and *number* is less than-30 or more than +29, addition is performed beginning at one minute.
118
-
119
116
- If *datepart* is **millisecond** and *number* is between -30001 and +29998, no addition is performed.
120
-
121
117
- If *datepart* is **millisecond** and *number* is less than -30001 or more than +29998, addition is performed beginning at one minute.
### C. Using expressions as arguments for the number and date parameters
242
-
The following examples use different types of expressions as arguments for the *number* and *date* parameters.
235
+
The following examples use different types of expressions as arguments for the *number* and *date* parameters. The examples use the AdventureWorks database.
243
236
244
237
#### Specifying a column as date
245
238
The following example adds `2` days to each value in the `OrderDate` column to derive a new column named `PromisedShipDate`.
#### Specifying scalar subqueries and scalar functions as number and date
317
305
The following example uses scalar subqueries, `MAX(ModifiedDate)`, as arguments for *number* and *date*. `(SELECT TOP 1 BusinessEntityID FROM Person.Person)` is an artificial argument for the number parameter to show how to select a *number* argument from a value list.
318
306
319
307
```
320
-
USE AdventureWorks2012;
321
-
GO
322
308
SELECT DATEADD(month,(SELECT TOP 1 BusinessEntityID FROM Person.Person),
The following example uses a ranking function as arguments for *number*.
335
321
336
322
```
337
-
USE AdventureWorks2012;
338
-
GO
339
323
SELECT p.FirstName, p.LastName
340
324
,DATEADD(day,ROW_NUMBER() OVER (ORDER BY
341
325
a.PostalCode),SYSDATETIME()) AS 'Row Number'
@@ -352,8 +336,6 @@ WHERE TerritoryID IS NOT NULL
352
336
The following example uses an aggregate window function as an argument for *number*.
353
337
354
338
```
355
-
USE AdventureWorks2012;
356
-
GO
357
339
SELECT SalesOrderID, ProductID, OrderQty
358
340
,DATEADD(day,SUM(OrderQty)
359
341
OVER(PARTITION BY SalesOrderID),SYSDATETIME()) AS 'Total'
@@ -362,125 +344,6 @@ WHERE SalesOrderID IN(43659,43664);
362
344
GO
363
345
```
364
346
365
-
## Examples: [!INCLUDE[ssSDWfull](../../includes/sssdwfull-md.md)] and [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
366
-
367
-
### D. Incrementing datepart by an interval of 1
368
-
Each of the following statements increments *datepart* by an interval of 1.
369
-
370
-
```
371
-
CREATE TABLE Customer (
372
-
ID INTEGER,
373
-
CustomerDate datetime2);
374
-
375
-
INSERT INTO Customer VALUES (1, '2007-01-01 13:10:10.1111111');
376
-
377
-
SELECT 'year', DATEADD(year,1,CustomerDate) FROM Customer
378
-
UNION ALL
379
-
SELECT 'quarter',DATEADD(quarter,1,CustomerDate) FROM Customer
380
-
UNION ALL
381
-
SELECT 'month',DATEADD(month,1,CustomerDate) FROM Customer
382
-
UNION ALL
383
-
SELECT 'dayofyear',DATEADD(dayofyear,1,CustomerDate) FROM Customer
384
-
UNION ALL
385
-
SELECT 'day',DATEADD(day,1,CustomerDate) FROM Customer
386
-
UNION ALL
387
-
SELECT 'week',DATEADD(week,1,CustomerDate) FROM Customer
388
-
UNION ALL
389
-
SELECT 'weekday',DATEADD(weekday,1,CustomerDate) FROM Customer
390
-
UNION ALL
391
-
SELECT 'hour',DATEADD(hour,1,CustomerDate) FROM Customer
392
-
UNION ALL
393
-
SELECT 'minute',DATEADD(minute,1,CustomerDate) FROM Customer
394
-
UNION ALL
395
-
SELECT 'second',DATEADD(second,1,CustomerDate) FROM Customer
396
-
UNION ALL
397
-
SELECT 'millisecond',DATEADD(millisecond,1,CustomerDate) FROM Customer
398
-
UNION ALL
399
-
SELECT 'microsecond',DATEADD(microsecond,1,CustomerDate) FROM Customer
400
-
UNION ALL
401
-
SELECT 'nanosecond',DATEADD(nanosecond,1,CustomerDate) FROM Customer;
402
-
```
403
-
404
-
`-------------- --------------------------`
405
-
406
-
`Year 01/01/2008 1:10:10.1111111`
407
-
408
-
`Quarter 04/01/2007 1:10:10.1111111`
409
-
410
-
`Month 02/01/2007 1:10:10.1111111`
411
-
412
-
`Dayofyear 01/02/2007 1:10:10.1111111`
413
-
414
-
`Day 01/02/2007 1:10:10.1111111`
415
-
416
-
`Week 01/08/2007 1:10:10.1111111`
417
-
418
-
`Weekday 01/02/2007 1:10:10.1111111`
419
-
420
-
`Hour 01/01/2007 2:10:10.1111111`
421
-
422
-
`Minute 01/01/2007 1:11:10.1111111`
423
-
424
-
`Second 01/01/2007 1:10:11.1111111`
425
-
426
-
`Millisecond 01/01/2007 1:10:10.1121111`
427
-
428
-
`Microsecond 01/01/2007 1:10:10.1111121`
429
-
430
-
`Nanosecond 01/01/2007 1:10:10.1111111`
431
-
432
-
### E. Using expressions as arguments for the number and date parameters
433
-
The following examples use different types of expressions as arguments for the *number* and *date* parameters.
434
-
435
-
#### Specifying column as date
436
-
The following example adds `60` days to each `HireDate` to calculate the date that the employee is eligible for company benefits.
437
-
438
-
```
439
-
-- Uses AdventureWorks
440
-
441
-
SELECT EmployeeKey
442
-
,HireDate
443
-
,DATEADD(day,60,HireDate) AS BenefitsDate
444
-
FROM DimEmployee;
445
-
```
446
-
447
-
#### Specifying scalar system function as date
448
-
The following example specifies `SYSDATETIME` for *date*.
449
-
450
-
```
451
-
-- Uses AdventureWorks
452
-
453
-
SELECT TOP (1) DATEADD(month, 1, SYSDATETIME()) FROM DimCustomer;
454
-
```
455
-
456
-
#### Specifying scalar subqueries and scalar functions as number and date
457
-
The following example uses scalar subqueries and scalar functions (`MAX(HireDate))`, as arguments for *number* and *date*. `(SELECT (TOP(1) EmployeeKey FROM dbo.DimEmployee)` is an artificial argument for the *number* parameter to show how to select a *number* argument from a value list.
458
-
459
-
```
460
-
-- Uses AdventureWorks
461
-
462
-
SELECT TOP(1) DATEADD (month, (SELECT TOP(1) EmployeeKey FROM dbo.DimEmployee),
463
-
(SELECT MAX(HireDate) FROM dbo.DimEmployee))
464
-
FROM dbo.DimCustomer;
465
-
```
466
-
467
-
#### Specifying constants as number and date
468
-
The following example uses numeric and character constants as arguments for *number* and *date*.
469
-
470
-
```
471
-
-- Uses AdventureWorks
472
-
473
-
SELECT TOP (1) DATEADD(minute, 1, '2007-05-07 09:53:01.123') FROM DimCustomer;
474
-
```
475
-
476
-
#### Specifying numeric expressions and scalar system functions as number and date
477
-
The following example uses a numeric expressions (-`(10/2))`, and a scalar system function (`SYSDATETIME`) as arguments for *number* and *date*.
478
-
479
-
```
480
-
-- Uses AdventureWorks
481
-
482
-
SELECT TOP (1) DATEADD(month,10/2, SYSDATETIME()) FROM DimCustomer;
483
-
```
484
347
485
348
## See Also
486
349
[CAST and CONVERT (Transact-SQL)](../../t-sql/functions/cast-and-convert-transact-sql.md)
0 commit comments