Definition and usage
The DATEADD () function adds or removes the specified time interval in the date.
Syntax
DATEADD(datepart,number,date)
DateA parameter is a valid date expression.NumberIs the number of intervals you want to add. For the future time, this number is a positive number, and for the past time, this number is a negative number.
DatepartParameters can be the following values:
| Datepart |
Abbreviations |
| Year |
Yy, yyyy |
| Quarter |
Qq, q |
| Month |
Mm, m |
| Day of the year |
Dy, y |
| Day |
Dd, d |
| Week |
Wk, ww |
| Week |
Dw, w |
| Hours |
Hh |
| Minutes |
Mi, n |
| Seconds |
Ss, s |
| Millisecond |
MS |
| Subtle |
Mcs |
| Nanoseconds |
Ns |
Instance
Suppose we have the following "Orders" table:
| OrderId |
ProductName |
OrderDate |
| 1 |
'Computer' |
16:25:46. 635 |
Now, we want to add 2 days to "OrderDate" so that we can find the payment date.
We use the following SELECT statement:
SELECT OrderId,DATEADD(day,2,OrderDate) AS OrderPayDateFROM Orders
Result:
| OrderId |
OrderPayDate |
| 1 |
16:25:46. 635 |