Definition and usage
DATEADD() function to add or subtract a specified time interval from a date.
Grammar
DATEADD(DatePart, Number, date)
The date parameter is a valid day expression. Numberis the number of intervals you want to add; for future time, this number is positive, and for the past time, this number is negative.
DatePartThe parameter can be the following value:
DatePartAbbreviation
Year yy, yyyy
Quarterly QQ, Q
month mm, M
The day of the year dy, y
Day DD, D
Zhou Wk, ww
Week DW, W
Hour HH
Minutes mi, n
SEC SS, S
Ms MS
Subtle MCs
nanosecond NS
Instance
Suppose we have the following "Orders" table:
OrderId ProductName OrderDate
1'Computer' -- A- in -: -:46.635
Now, we want to add the "OrderDate"2Day, so you can find the payment date.
We use the followingSELECTStatement:
SELECTOrderId,DATEADD( Day,2, OrderDate) asOrderpaydate
fromOrders
Results:
OrderId orderpaydate--Example: Select DateAdd (Month,-3,getdate ())--Returns a point in time three months ago, whichever is the current time select DATEADD (Yyyy,-3,getdate ())--Take the current time to return a point in time three years ago Select DateAdd (Month,3,getdate ())--Returns a point in time after three months, whichever is the current time
SQL queries data for the last three months (queries in recent days, years, etc.)