Calculate difference between two dates in SQL Server

How To Find Date Difference Between Two Dates ?

DATEDIFF ( datepart , startdate , enddate )
You can use SQL Server DateDiff function to calculate difference between two dates. This delay can be calculated in days, months, years, minutes,hours or seconds.




Datepart
Is the parameter that specifies on which part of the date to calculate the difference. The following table lists dateparts and abbreviations recognized by SQL Server 2005. These dateparts and abbreviations cannot be supplied as a user-declared variable.

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

Example 1:
Select datediff (m, '20100131', '20100201')
Example 2:
Select datediff(mi, '2010-01-22 15:29:55.090' , '2010-01-22 15:30:09.153') 
Example 3:
SELECT  DATEDIFF(day, '2010-01-22 15:29:55.090', '2010-01-22 15:30:09.153')

No comments:

Post a Comment