Today, a colleague found a SQL Server statement execution is particularly slow, after the inspection found that the condition plus the time period, before using the time field between XXX and xxx way, then change the time to determine the method,
----------Dafediff----------Return two points of difference, optional units YY,MM,DD,HH,SS, etc.
Query this year's data:
SELECT * from DateDiff (yy, Time field, GETDATE ()) =0
Query data for 2016 years:
SELECT * from DateDiff (yy, Time field, ' 2016 ') =0
----------Dafepart----------intercept a unit at a point in time, such as intercept year or month or day, optional unit YY,MM,DD,HH,SS, etc.
Query data for a certain period of time, such as February 2016 to December 2016:
SELECT * from DateDiff (yy, Time field, ' =0 ') and datepart (mm, Time field) >=2 and datepart (mm, Time field) <=12
SQL Server queries data based on time period