SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 DateDiff in HH:mm:ss
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

stharish
Starting Member

43 Posts

Posted - 03/02/2014 :  15:14:16  Show Profile  Reply with Quote
Given the two datetimes below, what's the best way to obtain the total duration in hours, minutes and seconds (HH:mm:ss)?

Start Time: 2014-03-02 20:55:00.000
End Time: 2014-03-03 07:00:00.000

Duration = 10:05:00

Thank you!

waterduck
Aged Yak Warrior

Malaysia
965 Posts

Posted - 03/02/2014 :  19:58:44  Show Profile  Reply with Quote

DECLARE @StartTime Datetime = '2014-03-02 20:55:00.000',
	@EndTime Datetime = '2014-03-03 07:00:00.000'
		
SELECT CONVERT(VARCHAR(12), @EndTime - @StartTime, 114) AS Duration
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 03/03/2014 :  04:10:00  Show Profile  Reply with Quote

DECLARE @StartTime Datetime = '2014-03-02 20:55:00.000',
	@EndTime Datetime = '2014-03-05 07:00:00.000'
		
SELECT CAST(DATEDIFF(seconds,@StartTime,@EndTime)/60*60 AS varchar(5)) + ':' 
+ RIGHT('00' + CAST((DATEDIFF(seconds,@StartTime,@EndTime)%3600)/60  AS  varchar(2)),2) + ':'
+ RIGHT('00' + CAST((DATEDIFF(seconds,@StartTime,@EndTime)%3600)%60  AS  varchar(2)),2)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs

Edited by - visakh16 on 03/03/2014 04:12:21
Go to Top of Page

maunishq
Yak Posting Veteran

Canada
71 Posts

Posted - 03/03/2014 :  13:20:50  Show Profile  Reply with Quote
DECLARE @StartTime Datetime = '2014-03-02 20:55:00.000',
@EndTime Datetime = '2014-03-03 07:00:00.000'

SELECT FORMAT(@EndTime-@StartTime, 'HH:mm:ss') AS Duration

!_(M)_!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 03/03/2014 :  14:48:29  Show Profile  Reply with Quote
quote:
Originally posted by maunishq

DECLARE @StartTime Datetime = '2014-03-02 20:55:00.000',
@EndTime Datetime = '2014-03-03 07:00:00.000'

SELECT FORMAT(@EndTime-@StartTime, 'HH:mm:ss') AS Duration

!_(M)_!


Wont work if dates span more than 24 hrs apart
ALso Format works only from SQL 2012 onwards

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 03/03/2014 :  16:49:05  Show Profile  Reply with Quote
quote:
Originally posted by visakh16


DECLARE @StartTime Datetime = '2014-03-02 20:55:00.000',
	@EndTime Datetime = '2014-03-05 07:00:00.000'
		
SELECT CAST(DATEDIFF(second,@StartTime,@EndTime)/3600 AS varchar(5)) + ':' 
+ RIGHT('00' + CAST((DATEDIFF(second,@StartTime,@EndTime)%3600)/60  AS  varchar(2)),2) + ':'
+ RIGHT('00' + CAST((DATEDIFF(second,@StartTime,@EndTime)%3600)%60  AS  varchar(2)),2)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs


Just a couple of small corrections.

Edited by - Lamprey on 03/03/2014 16:50:22
Go to Top of Page

stharish
Starting Member

43 Posts

Posted - 03/04/2014 :  00:44:29  Show Profile  Reply with Quote
This works great! Thank you all who replied!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.3 seconds. Powered By: Snitz Forums 2000