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
 General SQL Server Forums
 New to SQL Server Programming
 Decimal time to hours minutes

Author  Topic 

MichelleMabbs
Starting Member

24 Posts

Posted - 2014-02-03 : 08:01:57
Hi there i have a series of times in decimal 15 min slots

the data type is float and the field is the followng:

10
10.25
10.5
10.75
11
and so on

i would like to convert that to the hour and 15 minute slot

10:00:00
10:15:00
10:30:00
10:45:00
11:00:00

Any idea how to do this would be really grateful

Michelle

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2014-02-03 : 11:46:34
You didn't specify what datatype(s) you want the results in but this will do the math and return two INT columns for hour and (closest) minute. You could either use the hour/minute values to build a TIME result or convert the components to varchar and build a varchar value:

;with yourTable (t) as
(
select convert(float, 10) union all
select 10.25 union all
select 10.5 union all
select 10.75 union all
select 11 union all
select 11.11
)
select convert(int, floor(t)) as [Hour]
,convert(int, round(60 * (t - floor(t)),0)) as [Minute]
from yourTable

OUTPUT:
Hour Minute
----------- -----------
10 0
10 15
10 30
10 45
11 0
11 7


Be One with the Optimizer
TG
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-03 : 13:39:27
quote:
Originally posted by MichelleMabbs

Hi there i have a series of times in decimal 15 min slots

the data type is float and the field is the followng:

10
10.25
10.5
10.75
11
and so on

i would like to convert that to the hour and 15 minute slot

10:00:00
10:15:00
10:30:00
10:45:00
11:00:00

Any idea how to do this would be really grateful

Michelle



SELECT CONVERT(time,DATEADD(n,(Field-FLOOR(Field))*60,DATEADD(hh,FLOOR(Field),0)))
FROM Table


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

MichelleMabbs
Starting Member

24 Posts

Posted - 2014-02-04 : 04:49:47
thank you worked a treat

Michelle
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-04 : 06:08:57
welcome

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

- Advertisement -