Latest web development tutorials

SQL Server DATEDIFF () function

SQL Dates SQL Server Date Functions

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 Dates SQL Server Date Functions