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