How to Use the DATEDIFF() Function in SQL Server: A Detailed Guide
☰Fullscreen
Table of Content:
Syntax:
DATEDIFF(datepart, startdate, enddate)
DATEDIFF(datepart, startdate, enddate) - Returns the count of the specified datepart boundaries crossed between the specified startdate and enddate.
| DatePart | Abbreviations |
|---|---|
| year | yy, yyyy |
| quarter | qq, q |
| month | mm, m |
| dayofyear | dy, y |
| day | dd, d |
| week | wk, ww |
| weekday | dw |
| hour | hh |
| minute | mi, n |
| second | ss, s |
| millisecond | ms |
| microsecond | mcs |
| nanosecond | ns |
| TZoffset | tz |
| ISO_WEEK | isowk, isoww |
Code:
Select DATEDIFF(MONTH, '11/30/2005','01/31/2006') Select DATEDIFF(DAY, '11/30/2005','01/31/2006')
Output:
The above code will produce the following result-
2 62