Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL time difference between two dates result in hh:mm:ss

I am facing some difficulty with calculating the time difference between two dates.

What I want is, I have two dates let say

@StartDate = '10/01/2012 08:40:18.000' @EndDate='10/04/2012 09:52:48.000' 

so the difference between two dates in the form of hh:mm:ss is 72:42:30.

How can I get this result in a T-SQL query?

like image 445
Gnanasekaran Kuppusamy Avatar asked Nov 27 '12 05:11

Gnanasekaran Kuppusamy


People also ask

How do I get the time difference between two dates in SQL?

To find the difference between dates, use the DATEDIFF(datepart, startdate, enddate) function. The datepart argument defines the part of the date/datetime in which you'd like to express the difference. Its value can be year , quarter , month , day , minute , etc.

How do I get HH MM SS from date in SQL?

SELECT convert(varchar, getdate(), 108) outputs as hh:mm:ss .

How do I get time difference between two timestamps in seconds in SQL?

If you'd like to calculate the difference between the timestamps in seconds, multiply the decimal difference in days by the number of seconds in a day, which equals 24 * 60 * 60 = 86400 , or the product of the number of hours in a day, the number of minutes in an hour, and the number of seconds in a minute.


1 Answers

declare @StartDate datetime, @EndDate datetime  select @StartDate = '10/01/2012 08:40:18.000',@EndDate='10/04/2012 09:52:48.000'  select convert(varchar(5),DateDiff(s, @startDate, @EndDate)/3600)+':'+convert(varchar(5),DateDiff(s, @startDate, @EndDate)%3600/60)+':'+convert(varchar(5),(DateDiff(s, @startDate, @EndDate)%60)) as [hh:mm:ss] 

This query will helpful to you.

like image 58
Franklin Avatar answered Sep 23 '22 05:09

Franklin