SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Decimal Hours Duration to HH:MM


Decimal Hours Duration to HH:MM

Author
Message
m.dunster
m.dunster
SSC Eights!
SSC Eights! (878 reputation)SSC Eights! (878 reputation)SSC Eights! (878 reputation)SSC Eights! (878 reputation)SSC Eights! (878 reputation)SSC Eights! (878 reputation)SSC Eights! (878 reputation)SSC Eights! (878 reputation)

Group: General Forum Members
Points: 878 Visits: 266
Hi,

Anyone got a simple method for converting decimal time duration to HH:MM.

For example:

1.5 to 1:30
30.75 to 30:45
95.25 to 95:15

etc

Many thanks for any assistance.

Matthew
cnporteous
cnporteous
Mr or Mrs. 500
Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)

Group: General Forum Members
Points: 570 Visits: 122
Hey,

This will do it BUT - You arent getting days in that.

DECLARE @hours decimal(15,4)
SELECT @hours = 28.75
SELECT RIGHT('00' + CONVERT(varchar(2),FLOOR(@hours)),2)
+':'
+ RIGHT('00' + CONVERT(varchar(2),FLOOR(((@hours-FLOOR(@hours))*60))),2)
+':'
+ RIGHT('00' + CONVERT(varchar(2),FLOOR(((@hours-FLOOR(@hours))*60)-FLOOR(((@hours-FLOOR(@hours))*60)))*60),2)



Here's the thread from google. There's another method there too which will get you the full datetime data type too.

http://www.dbforums.com/microsoft-sql-server/1202305-converting-decimal-time.html

HTH

Ta
laurie-789651
laurie-789651
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3604 Visits: 1272


declare @time decimal(5,2);
set @time = 95.25;
select cast(@time as int)+(((@time-cast(@time as int))*.60));



Lowell
Lowell
SSC Guru
SSC Guru (183K reputation)SSC Guru (183K reputation)SSC Guru (183K reputation)SSC Guru (183K reputation)SSC Guru (183K reputation)SSC Guru (183K reputation)SSC Guru (183K reputation)SSC Guru (183K reputation)

Group: General Forum Members
Points: 183497 Visits: 41569
Here's my first guess at it; i left lots of stuff in there to look at to help understand it.

With PayRollHours (TheTime)
As
(
SELECT 1.5 UNION ALL
SELECT 30.75 UNION ALL
SELECT 40.00 UNION ALL
SELECT 95.25
)
SELECT DATEADD(minute,TotalMinutesInteger,'1900-01-01') ResultsAsDateTime,
CONVERT(VARCHAR(5),DATEADD(minute,TotalMinutesInteger,'1900-01-01'),108) ResultAsString,
*
FROM (
SELECT TheTime * 60 AS TotalMinutes,
CONVERT(int,TheTime * 60 ) As TotalMinutesInteger,
CONVERT(int,TheTime * 60 ) / 60 As HoursPortion,
CONVERT(int,TheTime * 60 ) %60 As MinutesPortion

FROM PayRollHours
) X



Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
cnporteous
cnporteous
Mr or Mrs. 500
Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)

Group: General Forum Members
Points: 570 Visits: 122
Thats handy Lowell, nice one. All options there for you OP
m.dunster
m.dunster
SSC Eights!
SSC Eights! (878 reputation)SSC Eights! (878 reputation)SSC Eights! (878 reputation)SSC Eights! (878 reputation)SSC Eights! (878 reputation)SSC Eights! (878 reputation)SSC Eights! (878 reputation)SSC Eights! (878 reputation)

Group: General Forum Members
Points: 878 Visits: 266
@Cnporteus, yes saw that one, didn't work for high enough hours though.

@Laurie - Thanks for this tweaked it to:

declare @time decimal(5,2);
set @time = 95.75
select replace(cast(convert(decimal(10,2),cast(@time as int)+(((@time-cast(@time as int))*.60))) as varchar),'.',':')



Which seems to work nicely

Thanks a lot.
m.dunster
m.dunster
SSC Eights!
SSC Eights! (878 reputation)SSC Eights! (878 reputation)SSC Eights! (878 reputation)SSC Eights! (878 reputation)SSC Eights! (878 reputation)SSC Eights! (878 reputation)SSC Eights! (878 reputation)SSC Eights! (878 reputation)

Group: General Forum Members
Points: 878 Visits: 266
Thanks to you too Lowell for your input. Squirelled away for later use I'm sure !

Matthew
laurie-789651
laurie-789651
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3604 Visits: 1272
m.dunster (8/30/2012)


@Laurie - Thanks for this tweaked it to:

declare @time decimal(5,2);
set @time = 95.75
select replace(cast(convert(decimal(10,2),cast(@time as int)+(((@time-cast(@time as int))*.60))) as varchar),'.',':')



Which seems to work nicely

Thanks a lot.



Even better!

BTW - I left a surplus pair of brackets in - removed below:



select replace(cast(convert(decimal(10,2),cast(@time as int)+((@time-cast(@time as int))*.60)) as varchar),'.',':')



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search