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:
The code is as follows: |
Copy code |
SELECT OrderId, DATEADD (day, 2, OrderDate) AS OrderPayDate FROM Orders |
Select DateAdd (Month,-3, getdate () -- Returns a time, based on which you can query the desired data.
-- Example:
The code is as follows: |
Copy code |
Select DateAdd (Month,-3, getdate () -- takes the current time as the standard and returns a time point three months ago. Select DateAdd (yyyy,-3, getdate () -- Returns a time point three years ago based on the current time. Select DateAdd (Month, 3, getdate () -- Returns a time point after three months based on the current time. |
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 |
-- Indicates the number of time intervals to be added. A numeric expression can be a positive number (to get the future date) or a negative number (to get the past date ).
-- Date is required. The comparison time during Query. getdate () indicates the current time in SQL.