Friday, November 18, 2011

Get the the difference between two dates in MS SQL

Facebook

The DATEDIFF() function returns the difference between two dates
Syntax: DATEDIFF ( datepart , startdate , enddate )

Following is an example for the DateDiff()
DECLARE @startdate datetime ='2011-11-14 02:00:06.957';
DECLARE @enddate datetime = '2011-11-16 05:00:15.490';
SELECT DATEDIFF(day, @startdate, @enddate) as dayDifference,DATEDIFF(HOUR, @startdate, @enddate)as hourDifference
Output



dayDifference hourDifference
2 51


Similarly the following are the available datepart Options on the DateDiff() Function.

year(or yy or yyyy)
quarter(or qq or q)
month (or mm or m )
dayofyear(or dy or y )
day (or dd, d )
week(or wk or ww)

hour(or hh)
minute(or mi or n)
second(or ss or s )
millisecond(or ms)
microsecond(or mcs)
nanosecond(or ns)
TZoffset(or tz)
ISO_WEEK(or isowk or isoww)

0 comments:

Post a Comment

I love to entertain onymous user Comment !

Pages

 ©mytechtoday.com 2006-2010

 ©Mytechtoday

TOP