Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

Thank you for your question,

SQL
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
 
Share this answer
 
v2
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)

SQL
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
 
Share this answer
 
v2
Please check the following:

http://www.sqlservercurry.com/2008/04/find-hours-minutes-and-seconds-in.html[^]
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900