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.
datepart | Abbreviations |
---|---|
year | yy, yyyy |
quarter | qq, q |
month | mm, m |
dayofyear | dy, y |
day | dd, d |
week | wk, ww |
hour | hh |
minute | mi, n |
second | ss, s |
millisecond | ms |
microsecond | mcs |
nanosecond | ns |
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