This is a mirror of official site: http://jasper-net.blogspot.com/

How to get difference between two dates – TSQL

| Tuesday, May 24, 2011
o calculate the difference between two dates, you can use DATEDIFF() function. The DATEDIFF() function returns the number of days/month/years and time between two dates.

Syntax:

DATEDIFF (date part, start date, end date)

 

For example, to calculate time left till Independence Day, you can use:

– © 2011 – Vishal (http://SqlAndMe.com)
 
DECLARE     @Today      DATETIME = GETDATE()
DECLARE     @IDay       DATETIME = '2011-08-15 08:30:00'
 
SELECT DATEDIFF(DAY, @Today, @IDay),      'Days Left'
UNION ALL
SELECT DATEDIFF(MONTH, @Today, @IDay),    'Months Left'
UNION ALL
SELECT DATEDIFF(YEAR, @Today, @IDay),     'Years Left'
UNION ALL
SELECT DATEDIFF(QUARTER, @Today, @IDay),  'Quarters Left'
UNION ALL
SELECT DATEDIFF(HOUR, @Today, @IDay),     'Hours Left'
UNION ALL
SELECT DATEDIFF(MINUTE, @Today, @IDay),   'Minutes Left'
UNION ALL
SELECT DATEDIFF(SECOND, @Today, @IDay),   'Seconds Left'

Result Set:

———– ————-
86          Days Left
3           Months Left

Read more: SQLServerPedia

Posted via email from Jasper-net

0 comments: