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) definition: Calculate 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 is named TableName, 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 query this month
SELECT * FROM TableName where DATEDIFF (Month,inputdate,getdate ()) =0 query last month
SELECT * FROM TableName where DATEDIFF (Month,inputdate,getdate ()) =1
Reprint Address: http://www.devdo.net/sql-server-query-date.html
SQL Server queries today, yesterday, this week, last week, this month, last month data