SQL Server queries the current day, last three days, this week, this month, the last one months, the data for this quarter's SQL statement

Source: Internet
Author: User
Tags getdate

That day:

SELECT * from T_news where DateDiff (Day,addtime,getdate ()) =0

Last three days:

SELECT * from T_news where DateDiff (Day,addtime,getdate ()) <= 2 and DateDiff (Day,addtime,getdate ()) >= 0

Week:

SELECT * from T_news WHERE (DATEPART (wk, addtime) = DATEPART (wk, GETDATE ())) and (DATEPART (yy, addtime) = DATEPART (yy, GET DATE ()))

Note: You cannot use the DateDiff difference at this time as 7, because DateDiff only represents the number of intervals

Month:

SELECT * from T_news WHERE (DATEPART (yy, addtime) = DATEPART (yy, GETDATE ())) and (DATEPART (mm, addtime) = DATEPART (mm, GET DATE ()))

Last one months:

SELECT * from T_news WHERE (DATEPART (yy, addtime) = DATEPART (yy, GETDATE ()))
and ((DATEPART (mm, addtime) = DATEPART (mm, GETDATE ())) OR (31-datepart (dd,addtime) +datepart (DD, GETDATE ())) <=31)

This quarter:

SELECT * from T_news where DATEPART (QQ, addtime) = DATEPART (QQ, GETDATE ()) and DATEPART (yy, addtime) = DATEPART (yy, Getdat E ())

SQL Server queries the current day, last three days, this week, this month, the last one months, the data for this quarter's SQL statement

Related Article

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.