Skip to content

Commit 7732602

Browse files
authored
DATEADD, formatting
1 parent 36a1fe6 commit 7732602

1 file changed

Lines changed: 8 additions & 145 deletions

File tree

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

Lines changed: 8 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "DATEADD (Transact-SQL) | Microsoft Docs"
33
ms.custom: ""
4-
ms.date: "03/14/2017"
4+
ms.date: "06/29/2017"
55
ms.prod: "sql-non-specified"
66
ms.reviewer: ""
77
ms.suite: ""
@@ -42,8 +42,6 @@ manager: "jhubbard"
4242
## Syntax
4343

4444
```
45-
-- Syntax for SQL Server, Azure SQL Database, Azure SQL Data Warehouse, Parallel Data Warehouse
46-
4745
DATEADD (datepart , number , date )
4846
```
4947

@@ -89,9 +87,10 @@ DATEADD (datepart , number , date )
8987

9088
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:
9189

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+
```
9594

9695
## number Argument
9796
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."`
@@ -113,11 +112,8 @@ SELECT DATEADD(year,-2147483647, '2006-07-31');
113112
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:
114113

115114
- If *datepart* is **second** and *number* is between -30 and +29, no addition is performed.
116-
117115
- If *datepart* is **second** and *number* is less than-30 or more than +29, addition is performed beginning at one minute.
118-
119116
- If *datepart* is **millisecond** and *number* is between -30001 and +29998, no addition is performed.
120-
121117
- If *datepart* is **millisecond** and *number* is less than -30001 or more than +29998, addition is performed beginning at one minute.
122118

123119
## Remarks
@@ -145,7 +141,6 @@ UNION ALL
145141
SELECT '50 nanoseconds', DATEADD(nanosecond,50,@datetime2)
146142
UNION ALL
147143
SELECT '150 nanoseconds', DATEADD(nanosecond,150,@datetime2);
148-
149144
```
150145

151146
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
@@ -158,14 +153,14 @@ SELECT '150 nanoseconds', DATEADD(nanosecond,150,@datetime2);
158153
49 nanoseconds 2007-01-01 13:10:10.1111111
159154
50 nanoseconds 2007-01-01 13:10:10.1111112
160155
150 nanoseconds 2007-01-01 13:10:10.1111113
161-
162156
```
163157

164158
## Time Zone Offset
165159
Addition is not allowed for time zone offset.
166160

167161
## Examples
168-
162+
163+
169164
### A. Incrementing datepart by an interval of 1
170165
Each of the following statements increments *datepart* by an interval of 1.
171166

@@ -196,7 +191,6 @@ UNION ALL
196191
SELECT 'microsecond',DATEADD(microsecond,1,@datetime2)
197192
UNION ALL
198193
SELECT 'nanosecond',DATEADD(nanosecond,1,@datetime2);
199-
200194
```
201195

202196
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
@@ -215,7 +209,6 @@ second 2007-01-01 13:10:11.1111111
215209
millisecond 2007-01-01 13:10:10.1121111
216210
microsecond 2007-01-01 13:10:10.1111121
217211
nanosecond 2007-01-01 13:10:10.1111111
218-
219212
```
220213

221214
### B. Incrementing more than one level of datepart in one statement
@@ -239,14 +232,12 @@ SELECT DATEADD(millisecond,1,@datetime2); --2007-01-01 01:01:01.110
239232
```
240233

241234
### 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.
243236

244237
#### Specifying a column as date
245238
The following example adds `2` days to each value in the `OrderDate` column to derive a new column named `PromisedShipDate`.
246239

247240
```
248-
USE AdventureWorks2012;
249-
GO
250241
SELECT SalesOrderID
251242
,OrderDate
252243
,DATEADD(day,2,OrderDate) AS PromisedShipDate
@@ -283,7 +274,6 @@ SalesOrderID OrderDate PromisedShipDate
283274
DECLARE @days int = 365,
284275
@datetime datetime = '2000-01-01 01:01:01.111'; /* 2000 was a leap year */;
285276
SELECT DATEADD(day, @days, @datetime);
286-
287277
```
288278

289279
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
@@ -293,7 +283,6 @@ SELECT DATEADD(day, @days, @datetime);
293283
2000-12-31 01:01:01.110
294284
295285
(1 row(s) affected)
296-
297286
```
298287

299288
#### Specifying scalar system function as date
@@ -310,15 +299,12 @@ SELECT DATEADD(month, 1, SYSDATETIME());
310299
2013-02-06 14:29:59.6727944
311300
312301
(1 row(s) affected)
313-
314302
```
315303

316304
#### Specifying scalar subqueries and scalar functions as number and date
317305
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.
318306

319307
```
320-
USE AdventureWorks2012;
321-
GO
322308
SELECT DATEADD(month,(SELECT TOP 1 BusinessEntityID FROM Person.Person),
323309
(SELECT MAX(ModifiedDate) FROM Person.Person));
324310
```
@@ -334,8 +320,6 @@ SELECT DATEADD(month,-(10/2), SYSDATETIME());
334320
The following example uses a ranking function as arguments for *number*.
335321

336322
```
337-
USE AdventureWorks2012;
338-
GO
339323
SELECT p.FirstName, p.LastName
340324
,DATEADD(day,ROW_NUMBER() OVER (ORDER BY
341325
a.PostalCode),SYSDATETIME()) AS 'Row Number'
@@ -352,8 +336,6 @@ WHERE TerritoryID IS NOT NULL
352336
The following example uses an aggregate window function as an argument for *number*.
353337

354338
```
355-
USE AdventureWorks2012;
356-
GO
357339
SELECT SalesOrderID, ProductID, OrderQty
358340
,DATEADD(day,SUM(OrderQty)
359341
OVER(PARTITION BY SalesOrderID),SYSDATETIME()) AS 'Total'
@@ -362,125 +344,6 @@ WHERE SalesOrderID IN(43659,43664);
362344
GO
363345
```
364346

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-
```
484347

485348
## See Also
486349
[CAST and CONVERT (Transact-SQL)](../../t-sql/functions/cast-and-convert-transact-sql.md)

0 commit comments

Comments
 (0)