In this post explains how to use the DATEDIFF function in SQL Server (Transact-SQL) with syntax and examples.
In SQL Server (Transact-SQL), the DATEDIFF function returns the difference between two date values, based on the interval specified.
The syntax for the DATEDIFF function in SQL Server (Transact-SQL) is:
The interval of time to use to calculate the difference between date1 and date2. It can be one of the following values:
Value (any one of) | Explanation |
---|---|
year, yyyy, yy | Year interval |
quarter, qq, q | Quarter interval |
month, mm, m | Month interval |
dayofyear | Day of year interval |
day, dy, y | Day interval |
week, ww, wk | Week interval |
weekday, dw, w | Weekday interval |
hour, hh | Hour interval |
minute, mi, n | Minute interval |
second, ss, s | Second interval |
millisecond, ms | Millisecond interval |
The two dates to calculate the difference between.
The DATEDIFF function can be used in the following versions of SQL Server (Transact-SQL):
Let's look at some SQL Server DATEDIFF function examples and explore how to use the DATEDIFF function in SQL Server (Transact-SQL).
For example: