Definition and usage
The DATEADD () function adds or subtracts a specified time interval from a date.
Grammar DateAdd (
DatePart,
Number,
Date)
The date parameter is a valid day expression. The number is the number of intervals you want to add, and for the future time, this is positive, and for the past time, this number is negative .
The datepart parameter can be the following value:
DatePart |
Abbreviations |
Years |
YY, yyyy |
Quarter |
QQ, Q |
Month |
MM, M |
The day of the year |
Dy, y |
Day |
DD, D |
Week |
WK, WW |
Week |
DW, W |
Hours |
hh |
Minutes |
MI, n |
Seconds |
SS, S |
Milliseconds |
Ms |
Subtle |
MCs |
Na-Sec |
Ns |
Instance
Suppose 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 "OrderDate" so we can find the payment date.
We use the following SELECT statement:
SELECT OrderId,
DATEADD(day,2,OrderDate)
As Orderpaydatefrom Orders
Results:
OrderId |
orderpaydate |
1 |
2008-12-31 16:25:46.635 |
Source: >
Select top 1 ttime as StartTime from Bbstopic ORDER BY ttime Desc--endtime
SELECT DateAdd (day,-7, ' 2014/5/22 11:55:00 ') as endtime--StartTime (one week)
SELECT DateAdd (day,-1, ' 2014/5/22 11:55:00 ') as StartTime--one day
SELECT DateAdd (day,-2, ' 2014/5/22 11:55:00 ') as StartTime--two days
SELECT DateAdd (month,-1, ' 2014/5/22 11:55:00 ') as StartTime--one months
SELECT DateAdd (month,-3, ' 2014/5/22 11:55:00 ') as StartTime--three months