Tuesday, 24 May 2016

DATEDIFF function in SQL server ?

To return the count of Specifed Datepart between the startdate and enddate in SQL server,we can use the DATEDIFF function.

SYNTAX

DATEDIFF (DATEPART,STARTDATE,ENDDATE)

You can find the datepart and its abbreviations below.
datepartAbbreviations
yearyy, yyyy
quarterqq, q
monthmm, m
dayofyeardy, y
daydd, d
weekwk, ww
hourhh
minutemi, n
secondss, s
millisecondms
microsecondmcs
nanosecondns
STARTDATE - Refers to start date
ENDDATE - Refers to end date

Its return type is INT.

EXAMPLE

You can refer the examples in the following screenshot.



You can also get the (milli/micro/nano) seconds difference between two dates.

Note : If the return value exceeds the INT maximum value, then the error will be returned.

Error :

Msg 535, Level 16, State 0, Line 3
The datediff function resulted in an overflow. The number of dateparts separating two date/time instances is too large. Try to use datediff with a less precise datepart.

No comments:

Post a Comment