SQL queries data for the last three months (queries in recent days, years, etc.)

Source: Internet
Author: User
Tags getdate

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.)

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.