SQL Server DATEDIFF () function
Definition and Usage
DATEDIFF () function returns the number of days between two dates.
grammar
DATEDIFF(datepart,startdate,enddate)
startdate and enddate parameters are legitimate date expression. datepart parameter can have the following values:
datepart | 缩写 |
---|---|
年 | yy, yyyy |
季度 | qq, q |
月 | mm, m |
年中的日 | dy, y |
日 | dd, d |
周 | wk, ww |
星期 | dw, w |
小时 | hh |
分钟 | mi, n |
秒 | ss, s |
毫秒 | ms |
微妙 | mcs |
纳秒 | ns |
Examples
Now we want to get the number of days between two dates.
We use the following SELECT statement:
SELECT DATEDIFF(day,'2008-06-05','2008-08-05') AS DiffDate
result:
DiffDate |
---|
61 |
Examples
Now we want to get the number of days between two dates (please note that the second date is earlier than the first date, the result is a negative number).
We use the following SELECT statement:
SELECT DATEDIFF(day,'2008-08-05','2008-06-05') AS DiffDate
result:
DiffDate |
---|
-61 |
SQL Server Date Functions