Course Recommendation:

SQL Server DATEDIFF() Function

Definition and Usage

The DATEDIFF() function returns the time between two dates.

SyntaxdatepartDATEDIFF()DATEDIFF(and,

) startdate and The parameter is a valid date expression.

datepart The parameter can be one of the following values:

datepart Abbreviation
Years yy, yyyy
Quarters qq, q
Months mm, m
Days in the year dy, y
Days dd, d
Weeks wk, ww
Weeks dw, w
Hours hh
Minutes mi, n
Seconds ss, s
Milliseconds ms
Microseconds mcs
Nanoseconds ns

Instance

Example 1

Use the following SELECT statement:

SELECT DATEDIFF(day,'2008-12-29','2008-12-30') AS DiffDate

Result:

DiffDate
1

Example 2

Use the following SELECT statement:

SELECT DATEDIFF(day,'2008-12-30','2008-12-29') AS DiffDate

Result:

DiffDate
-1