SQL Server DATEDIFF()
Function
Definition and Usage
The DATEDIFF()
function returns the number of days between two dates.
Syntax
The startdate
and enddate
parameters are valid date expressions. The datepart
parameter can be one of the following values:
datepart | Abbreviation |
---|---|
Year | yy, yyyy |
Quarter | qq, q |
Month | mm, m |
Day of Year | dy, y |
Day | dd, d |
Week | wk, ww |
Weekday | dw, w |
Hour | hh |
Minute | mi, n |
Second | ss, s |
Millisecond | ms |
Microsecond | mcs |
Nanosecond | ns |
Example
Now we want to get the number of days between two dates.
We use the following SELECT statement:
Result:
DiffDate |
---|
61 |
Example
Now we want to get the number of days between two dates (note that the second date is earlier than the first, resulting in a negative number).
We use the following SELECT statement:
Result:
DiffDate |
---|
-61 |