SQL Query--Date conditions (today, yesterday, this week, this month ...) )

Source: Internet
Author: User
Tags getdate

The main use of SQL functions

DATEDIFF (datepart,startdate,enddate)

SQL statement with database table TableA (date field ddate)

--Query today

SELECT * from TableA where DateDiff (dd,voucherdate,getdate ()) = 0

--Query yesterday

SELECT * from TableA where DateDiff (dd,voucherdate,getdate ()) = 1

--Query this week

SELECT * from TableA where DateDiff (Dd,voucherdate,getdate ()) <=7

--Query last week

SELECT * from TableA where DateDiff (Dd,voucherdate,getdate ()) >7 and DateDiff (Dd,voucherdate,getdate ()) <=14

--Query this month

SELECT * from TableA where DateDiff (mm,voucherdate,getdate ()) = 0

--Query last month

SELECT * from TableA where DateDiff (mm,voucherdate,getdate ()) = 1

--Check this year

SELECT * from TableA where DateDiff (yy,voucherdate,getdate ()) = 0

--Query last year

SELECT * from TableA where DateDiff (yy,voucherdate,getdate ()) = 1

SQL Query--Date conditions (today, yesterday, this week, this month ...) )

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.