«

»

Apr 09

how to find difference between two dates in sql server 2008

To find the difference between two dates in SQLSERVER 2008 we have to use DATEDIFF() function as below

ex:-

[code language=”sql”]

DECLARE     @Today      DATETIME = GETDATE()

DECLARE     @IDay       DATETIME = ‘2013-07-31 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’
[/code]

o/p:-

diffrence

Leave a Reply

Your email address will not be published. Required fields are marked *

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