SQL Time Difference

 Subtracting Date Time in MSSQL

–YEAR
SELECT DATEDIFF(year, ‘2012-12-31’, ‘2013-01-01’)

–Quarter
SELECT DATEDIFF(quarter, ‘2012-01-01 23:59:59’, ‘2013-01-01 00:00:00’)

–Month
SELECT DATEDIFF(month, ‘2012-12-31 23:59:59.9999999’, ‘2013-02-01 00:00:00.0000000’)

–Day
SELECT DATEDIFF(day, ‘2012-12-01 23:59:59.9999999’, ‘2012-12-31 00:00:00.0000000’)

–Week
SELECT DATEDIFF(week, ‘2013-06-01 23:59:59.9999999’, ‘2013-06-30 00:00:00.0000000’)

–Hour
SELECT DATEDIFF(hour, ‘2013-06-30 08:30:00.0000000’, ‘2013-06-30 17:30:00.0000000’)

–Minute
SELECT DATEDIFF(minute, ‘2013-06-30 08:30:00.0000000’, ‘2013-06-30 17:30:00.0000000’)

–Second
SELECT DATEDIFF(second, ‘2013-06-30 08:30:00.0000000’, ‘2013-06-30 08:30:25.0000000’)

–Millisecond
SELECT DATEDIFF(millisecond, ‘2005-12-31 23:59:59.9999999’, ‘2006-01-01 00:00:00.0000000’);

Comments

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>