Grammar:
DATEDIFF (datepart,startdate,enddate)
The datepart parameter can be the following value:
Date-part:year | Quarter | Month | Week | Day | Hour | Minute | Second | Millisecond
The parameter date-part specifies the date portion of the interval to be measured.
DatePart |
Abbreviations |
Years |
YY, yyyy |
Quarter |
QQ, Q |
Month |
MM, M |
The day of the year |
Dy, y |
Day |
DD, D |
Week |
WK, WW |
Week |
DW, W |
Hours |
hh |
Minutes |
MI, n |
Seconds |
SS, S |
Milliseconds |
Ms |
Subtle |
MCs |
Na-Sec |
Ns |
e.g.:
Choose:
The following statement returns 1:select DateDiff (hour, ' 4:00AM ', ' 5:50am ')
The following statement returns 102:select DateDiff (month, ' 1987/05/02 ', ' 1995/11/15 ')
The following statement returns 0:select DateDiff (day, ' 00:00 ', ' 23:59 ')
The following statement returns 4:select DateDiff (day, ' 1999/07/19 00:00 ', ' 1999/07/23 23:59 ')
The following statement returns 0:select DateDiff (month, ' 1999/07/19 ', ' 1999/07/23 ')
The following statement returns 1:select DateDiff (month, ' 1999/07/19 ', ' 1999/08/23 ')
Conditional statements:
SELECT * from MyDate where DateDiff (Day,datetime, ' 2016/05/06 14:30:00 ') =0
SQL Server Date Processing DateDiff