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
Reprint Address: http://www.devdo.net/sql-server-query-date.html
Merge two tables simultaneously query
For example:
Table A has field ID name age
Table B has field ID sex address
Query Sql:select * FROM (select Id,name,age,null sex,null address from a UNION ALL select Id,null name,null age,sex,address From B)
Principle: Let two tables have the same field name, null sex==null as sex, because the a table has no sex column, so we define a null column. This will have the same column as the B table.
UNION ALL is a query for all data displayed for two tables.
SQL Server calculates a part of the time difference function "Go"