The SQL DateAdd function queries data from recent years and months.

Source: Internet
Author: User
Tags current time getdate

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.