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
Returns a specified *date* with the specified *number*interval (signed integer) added to a specified *datepart* of that *date*.
38
+
This function adds a specified *number*value (as a signed integer) to a specified *datepart* of an input *date* value, and then returns that modified value.
39
39
40
-
For an overview of all [!INCLUDE[tsql](../../includes/tsql-md.md)] date and time data types and functions, see [Date and Time Data Types and Functions (Transact-SQL)](../../t-sql/functions/date-and-time-data-types-and-functions-transact-sql.md).
40
+
See [Date and Time Data Types and Functions (Transact-SQL)](../../t-sql/functions/date-and-time-data-types-and-functions-transact-sql.md) for an overview of all [!INCLUDE[tsql](../../includes/tsql-md.md)] date and time data types and functions.
41
41
42
42
[Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
43
43
@@ -49,7 +49,10 @@ DATEADD (datepart , number , date )
49
49
50
50
## Arguments
51
51
*datepart*
52
-
Is the part of *date* to which an **integer***number* is added. The following table lists all valid *datepart* arguments. User-defined variable equivalents are not valid.
52
+
The part of *date* to which `DATEADD` adds an **integer***number*. This table lists all valid *datepart* arguments.
53
+
54
+
> [!NOTE]
55
+
> `DATEADD` does not accept user-defined variable equivalents for the *datepart* arguments.
53
56
54
57
|*datepart*|Abbreviations|
55
58
|---|---|
@@ -68,15 +71,22 @@ Is the part of *date* to which an **integer***number* is added. The following ta
68
71
|**nanosecond**|**ns**|
69
72
70
73
*number*
71
-
Is an expression that can be resolved to an [int](../../t-sql/data-types/int-bigint-smallint-and-tinyint-transact-sql.md) that is added to a *datepart* of *date*. User-defined variables are valid.
72
-
If you specify a value with a decimal fraction, the fraction is truncated and not rounded.
74
+
An expression that can resolve to an [int](../../t-sql/data-types/int-bigint-smallint-and-tinyint-transact-sql.md) that `DATEADD` adds to a *datepart* of *date*. `DATEADD` accepts user-defined variable values for *number*. `DATEADD` will truncate a specified *number* value that has a decimal fraction. It will not round the *number* value in this situation.
73
75
74
76
*date*
75
-
Is an expression that can be resolved to a **time**, **date**, **smalldatetime**, **datetime**, **datetime2**, or **datetimeoffset** value. *date* can be an expression, column expression, user-defined variable, or string literal. If the expression is a string literal, it must resolve to a **datetime**. To avoid ambiguity, use four-digit years. For information about two-digit years, see [Configure the two digit year cutoff Server Configuration Option](../../database-engine/configure-windows/configure-the-two-digit-year-cutoff-server-configuration-option.md).
77
+
An expression that can resolve to one of the following values:
78
+
79
+
+**date**
80
+
+**datetime**
81
+
+**datetimeoffset**
82
+
+**datetime2**
83
+
+**smalldatetime**
84
+
+**time**
85
+
86
+
For *date*, `DATEADD` will accept a column expression, expression, string literal, or user-defined variable. A string literal value must resolve to a **datetime**. 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.
76
87
77
88
## Return types
78
-
The return data type is the data type of the *date* argument, except for string literals.
79
-
The return data type for a string literal is **datetime**. An error will be raised if the string literal seconds scale is more than three positions (.nnn) or contains the time zone offset part.
89
+
The *date* argument data type becomes the `DATEADD` return value data type, except for string literal *date* values. For a string literal, `DATEADD` returns a **datetime** value. `DATEADD` will raise an error if the string literal seconds scale exceeds three decimal place positions (.nnn) or if the string literal contains the time zone offset part.
80
90
81
91
## Return Value
82
92
@@ -85,45 +95,58 @@ The return data type for a string literal is **datetime**. An error will be rais
85
95
86
96
Each *datepart* and its abbreviations return the same value.
87
97
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:
98
+
If the following are true:
99
+
100
+
+*datepart* is **month**
101
+
+ the *date* month has more days than the return month
102
+
+ the *date* day does not exist in the return month
103
+
104
+
Then, `DATEADD` returns the last day of the return month. For example, September has 30 (thirty) days; therefore, these statements return 2006-09-30 00:00:00.000:
89
105
90
106
```sql
91
107
SELECT DATEADD(month, 1, '20060830');
92
108
SELECT DATEADD(month, 1, '20060831');
93
109
```
94
110
95
111
## number Argument
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."`
112
+
The *number* argument cannot exceed the range of **int**. In the following statements, the argument for *number* exceeds the range of **int** by 1. These statements both return the following error message: "`Msg 8115, Level 16, State 2, Line 1. Arithmetic overflow error converting expression to data type int."`
97
113
98
114
```sql
99
115
SELECT DATEADD(year,2147483648, '20060731');
100
116
SELECT DATEADD(year,-2147483649, '20060731');
101
117
```
102
118
103
119
## date Argument
104
-
The *date* argument cannot be incremented to a value outside the range of its data type. In the following statements, the *number* value that is added to the *date* value exceeds the range of the *date* data type. The following error message is returned: "`Msg 517, Level 16, State 1, Line 1 Adding a value to a 'datetime' column caused overflow`."
120
+
`DATEADD` will not accept a *date* argument incremented to a value outside the range of its data type. In the following statements, the *number* value added to the *date* value exceeds the range of the *date* data type. `DATEADD` returns the following error message: "`Msg 517, Level 16, State 1, Line 1 Adding a value to a 'datetime' column caused overflow`."
105
121
106
122
```sql
107
123
SELECT DATEADD(year,2147483647, '20060731');
108
124
SELECT DATEADD(year,-2147483647, '20060731');
109
125
```
110
126
111
127
## Return Values for a smalldatetime date and a second or Fractional Seconds datepart
112
-
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:
113
-
- If *datepart* is **second** and *number* is between -30 and +29, no addition is performed.
114
-
- If *datepart* is **second** and *number* is less than-30 or more than +29, addition is performed beginning at one minute.
115
-
- If *datepart* is **millisecond** and *number* is between -30001 and +29998, no addition is performed.
116
-
- If *datepart* is **millisecond** and *number* is less than -30001 or more than +29998, addition is performed beginning at one minute.
128
+
The seconds part of a [smalldatetime](../../t-sql/data-types/smalldatetime-transact-sql.md) value is always 00. For a **smalldatetime***date* value, the following apply:
129
+
130
+
- For a *datepart* of **second**, and a *number* value between -30 and +29, `DATEADD` makes no changes.
131
+
- For a *datepart* of **second**, and a *number* value less than -30, or more than +29, `DATEADD` performs its addition beginning at one minute.
132
+
- For a *datepart* of **millisecond** and a *number* value between -30001 and +29998, `DATEADD` makes no changes.
133
+
- For a *datepart* of **millisecond** and a *number* value less than -30001, or more than +29998, `DATEADD` performs its addition beginning at one minute.
117
134
118
135
## Remarks
119
-
DATEADD can be used in the SELECT \<list>, WHERE, HAVING, GROUP BY and ORDER BY clauses.
136
+
Use `DATEADD` in the following clauses:
137
+
138
+
+ GROUP BY
139
+
+ HAVING
140
+
+ ORDER BY
141
+
+ SELECT \<list>
142
+
+ WHERE
120
143
121
144
## Fractional seconds precision
122
-
Addition for a *datepart* of **microsecond** or **nanosecond** for *date* data types **smalldatetime**, **date**, and **datetime** is not allowed.
145
+
`DATEADD` does not allow addition for a *datepart* of **microsecond** or **nanosecond** for *date* data types **smalldatetime**, **date**, and **datetime**.
123
146
124
-
Milliseconds have a scale of 3 (.123), microseconds have a scale of 6 (.123456), And nanoseconds have a scale of 9 (.123456789). The **time**, **datetime2**, and **datetimeoffset** data types have a maximum scale of 7 (.1234567). If *datepart*is**nanosecond**, *number* must be 100 before the fractional seconds of *date* increase. A *number* between 1 and 49 is rounded down to 0 and a number from 50 to 99 is rounded up to 100.
147
+
Milliseconds have a scale of 3 (.123), microseconds have a scale of 6 (.123456), and nanoseconds have a scale of 9 (.123456789). The **time**, **datetime2**, and **datetimeoffset** data types have a maximum scale of 7 (.1234567). For a *datepart*of**nanosecond**, *number* must be 100 before the fractional seconds of *date* increase. A *number* between 1 and 49 will round down to 0, and a number from 50 to 99 rounds up to 100.
125
148
126
-
The following statements add a *datepart* of **millisecond**, **microsecond**, or **nanosecond**.
149
+
These statements add a *datepart* of **millisecond**, **microsecond**, or **nanosecond**.
### C. Using expressions as arguments for the number and date parameters
233
-
The following examples use different types of expressions as arguments for the *number* and *date* parameters. The examples use the AdventureWorks database.
256
+
These examples use different types of expressions as arguments for the *number* and *date* parameters. The examples use the AdventureWorks database.
234
257
235
258
#### Specifying a column as date
236
-
The following example adds `2` days to each value in the `OrderDate` column to derive a new column named `PromisedShipDate`.
259
+
This example adds `2`(two) 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
303
-
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.
327
+
This example uses scalar subqueries, `MAX(ModifiedDate)`, as arguments for *number* and *date*. `(SELECT TOP 1 BusinessEntityID FROM Person.Person)`serves as an artificial argument for the number parameter, to show how to select a *number* argument from a value list.
304
328
305
329
```sql
306
330
SELECT DATEADD(month,(SELECT TOP 1 BusinessEntityID FROMPerson.Person),
307
331
(SELECTMAX(ModifiedDate) FROMPerson.Person));
308
332
```
309
333
310
334
#### Specifying numeric expressions and scalar system functions as number and date
311
-
The following example uses a numeric expression (-`(10/2))`, [unary operators](../../mdx/unary-operators.md) (`-`), an [arithmetic operator](../../mdx/arithmetic-operators.md) (`/`), and scalar system functions (`SYSDATETIME`) as arguments for *number* and *date*.
335
+
This example uses a numeric expression (-`(10/2))`, [unary operators](../../mdx/unary-operators.md) (`-`), an [arithmetic operator](../../mdx/arithmetic-operators.md) (`/`), and scalar system functions (`SYSDATETIME`) as arguments for *number* and *date*.
312
336
313
337
```sql
314
338
SELECT DATEADD(month,-(10/2), SYSDATETIME());
315
339
```
316
340
317
341
#### Specifying ranking functions as number
318
-
The following example uses a ranking function as arguments for *number*.
342
+
This example uses a ranking function as an argument for *number*.
319
343
320
344
```sql
321
345
SELECTp.FirstName, p.LastName
@@ -331,7 +355,7 @@ WHERE TerritoryID IS NOT NULL
331
355
```
332
356
333
357
#### Specifying an aggregate window function as number
334
-
The following example uses an aggregate window function as an argument for *number*.
358
+
This example uses an aggregate window function as an argument for *number*.
0 commit comments