SQL Server DATEADD() Function
Definition and Usage
The DATEADD() function adds or subtracts a specified time interval from a date.
Syntax
DATEADD(datepartnumber
The parameter is a valid date expression.number Is the number of intervals you want to add; for future time, this number is positive, and for past time, this number is negative.
datepart The parameter can be one of the following values:
datepart | Abbreviations |
---|---|
Years | yy, yyyy |
Quarters | qq, q |
Months | mm, m |
Day of the year | dy, y |
Days | dd, d |
Weeks | wk, ww |
Weeks | dw, w |
Hours | hh |
Minutes | mi, n |
Seconds | ss, s |
Milliseconds | ms |
Microseconds | mcs |
Nanoseconds | ns |
Example
Assuming we have the following "Orders" table:
OrderId | ProductName | OrderDate |
---|---|---|
1 | 'Computer' | 2008-12-29 16:25:46.635 |
Now, we want to add 2 days to the "OrderDate" so that we can find the payment date.
We use the following SELECT statement:
SELECT OrderId,DATEADD(day,2,OrderDate) AS OrderPayDate FROM Orders
Result:
OrderId | OrderPayDate |
---|---|
1 | 2008-12-31 16:25:46.635 |