Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Returning DATEDIFF Value As hh:mm:ss
Message
 
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
00635803
Message ID:
00635825
Views:
12
This message has been marked as the solution to the initial question of the thread.
>>>DATEDIFF(hh,dtStart,dtEnd) returns a value in whole integer hours only.
>>>
>>>How do I return the value expressed as hh:mm:ss ?
>>>
>>>Thanks for your help.
>>>
>>
>>Try
CONVERT(char(8), DATEADD(second, DATEDIFF(second, dtStart, dtEnd), '20020101'), 8)
>
>Sergey,
>
>Yes. That works well up to 23:59:59 as in the following.
>
>
>declare @dtStart datetime
>declare @dtEnd datetime
>set @dtstart = '01/01/2002 9:00:00'
>set @dtEnd = '01/02/2002 8:49:00'
>SELECT CONVERT(char(8), DATEADD(second, DATEDIFF(second, @dtStart, @dtEnd), '20020101'), 8)
>
>
>I really didn't ask it right. I had in mind something that would calculate for any number of hours.
>
>Something that would present partial hours as decimals would be useful for my task also. For example, where 25 hours and 15 minutes = 25.25 hours.
>
>I'm glad that you're thinking well. Thank you! Thank you!

It's easier.
declare @dtStart datetime
declare @dtEnd datetime
set @dtstart = '01/01/2002 9:00:00'
set @dtEnd = '01/03/2002 8:09:00'
SELECT CAST(DATEDIFF(second, @dtStart, @dtEnd)*1.00/3600  AS numeric(10,2))

-- or
SELECT CAST(DATEDIFF(hh, @dtStart, @dtEnd) AS varchar(4) ) + 
	RIGHT(CONVERT(char(8), DATEADD(second, DATEDIFF(second, @dtStart, @dtEnd), '20020101'), 8),6)
--sb--
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform