Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 convert varchar to dayhoumminsec

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2011-02-11 : 09:20:39
Hi,
Imagine I have a field called field1 as varchar(50)
Let's say it coontains seconds i.e. 173136.67
This should represent (173136.67/3600) = 48.09 hours
How can I convert to day:hour:min:sec
i.e. 02:??:??...

At present I am using the following query but it shows an incorrect result
select LEFT(RIGHT(CONVERT(VARCHAR(23), DATEADD(SECOND, 173136.67, 0), 121), 12),8)
shows: 00:05:36

Any thoughts please?

Thanks

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-02-11 : 09:36:40
[code]
select convert(varchar(10), datediff(day, 0, dateadd(second, 173136.67, 0)))
+ ':' +
convert(varchar(10), dateadd(second, 173136.67, 0), 108) as ddhhmmss
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-02-11 : 09:40:17


declare @t decimal(5,2)
set @t=48.09
select DATEDIFF(DAY,0,DATE),CONVERT(char(12),date,108) from
(
select DATEADD(second,cast(@t as int)*3600+(@t*100%100)*60,0) as date
) as t

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2011-02-11 : 09:47:20
Solved.
Thank you all.
Go to Top of Page
   

- Advertisement -