Click here to Skip to main content
14,667,121 members
Rate this:
Please Sign up or sign in to vote.
Hi friends,
How do we find date diffrence between two dates?
I want difference like this : d(days):HH(hours):MM(Mins)
I have two date columns and want to get difference between them in the given format.
I use sql server 2008
Any help will be appreciated.
Regards,
Lok..
Posted

Rate this:
Please Sign up or sign in to vote.

Solution 2

Thank you for your question,

DECLARE @StartDate datetime
DECLARE @EndDate datetime

SELECT @StartDate=Date1 FROM MyDate
SELECT @EndDate=Date2 FROM MyDate

SELECT CONVERT(VARCHAR(2),ABS(DATEDIFF(dd,@StartDate,@EndDate)))
+' ' +
CONVERT(VARCHAR(2),ABS(DATEDIFF(hh,@StartDate,@EndDate)))
+' ' +
CONVERT(VARCHAR(2),ABS(DATEDIFF(MM,@StartDate,@EndDate)))


Thanks,
Mamun
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 3

You can use following to find difference between BDate and JDate
CONVERT(nvarchar, DATEDIFF(d, BDate, JDate)) + ':' + CONVERT(nvarchar, DATEDIFF(hh, BDate, JDate) % 24) + ':' + CONVERT(nvarchar, DATEDIFF(n, BDate, JDate) % 60)

SELECT
    ID, 
    Name, 
    BDate, 
    JDate, 
    CONVERT(nvarchar, DATEDIFF(d, BDate, JDate)) + ':' + CONVERT(nvarchar, DATEDIFF(hh, BDate, JDate) % 24) + ':' + CONVERT(nvarchar, DATEDIFF(n, BDate, JDate) % 60) AS Difference
FROM
    Temp
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

Please check the following:

http://www.sqlservercurry.com/2008/04/find-hours-minutes-and-seconds-in.html[^]
   
Comments
Lokesh Zende 7-Feb-11 8:51am
   
Ty Omi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100