SQL Server gets the data for today, yesterday, this week, last week, this month, and last month in the table

Source: Internet
Author: User
Tags getdate

The DateDiff () function and the GETDATE () function are needed when you are doing SQL Server development and sometimes need to get the data in the table today, yesterday, this week, last week, this month, and last month.
DATEDIFF (DatePart, StartDate, EndDate)
Explanation: Calculating the time difference
Datepare Value: Year | Quarter | Month | Week | Day | Hour | Minute | Second | Millisecond
StartDate: Start date
EndDate: End Date
GetDate ()
Explanation: Get the current system date

In the following example, the table name is TableName and the condition field is named Inputdate
Query today

SELECT * FROM TableName where DATEDIFF (Day,inputdate,getdate ()) =0
Query yesterday

SELECT * FROM TableName where DATEDIFF (Day,inputdate,getdate ()) =1
Query this week

SELECT * FROM TableName where DATEDIFF (Week,inputdate,getdate ()) =0
Query last week

SELECT * FROM TableName where DATEDIFF (Week,inputdate,getdate ()) =1
Check this month

SELECT * FROM TableName where DATEDIFF (Month,inputdate,getdate ()) =0
Query last month

SELECT * FROM TableName where DATEDIFF (Month,inputdate,getdate ()) =1

SQL Server gets the data for today, yesterday, this week, last week, this month, and last month in the table

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.