Link to home
Start Free TrialLog in
Avatar of HLRosenberger
HLRosenbergerFlag for United States of America

asked on

Convert Hours and Minutes into a Double.

I would like to convert hours/minutes  stored as varchar into a double, in hours.  0:15 is 15 minutes; 0:30 is 30 minutes, 1:20 is 1 hour and 20 minutes, etc.    

0:15 --> .25
0:30 --> .50
1:20 --> 1.33
1:30 --> 1.50
1:45 --> 1.75

Is there an easy way to do this?
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

As your question is being answered (by me or others), take a look at a wonderful article everything related to dates at:

https://www.experts-exchange.com/articles/12315/SQL-Server-Date-Styles-formats-using-CONVERT.html

Mike
Avatar of SStory
If vb.net:

Dim Parts() as string=YourTimeDataString.split(":")
'parts(0) will have hours
'parts(1) will have minutes
Dim Hours as Decimal=CDec(parts(0)) + CDec(parts(1))/60

Now if you want to change it in SQL itself, that is a different matter.
Avatar of HLRosenberger

ASKER

yes, I was looking to do this using SQL.
ASKER CERTIFIED SOLUTION
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK.  Since you had it tags as vb.net/SQL Server, etc and didn't specify, there was no way I could know.  It appears that Brian has shown you the same thing using TSQL. It is the same basic idea.  Take the minutes part and divide by 60 to get into decimal and add that to the hours part.
No problem.  I inadvertently included vb.net tag.
Brian - this worked, thanks.

SELECT CAST(SUBSTRING(@Time, 1, CHARINDEX(':', @Time) - 1) AS FLOAT) +
      CAST(SUBSTRING(@Time, CHARINDEX(':', @Time) + 1, LEN(@Time)) AS FLOAT) / 60

Is there a easy way to round the div by 60 result to 2 decimal places?
ROUND()  works for rounding.  Thanks!@
Another option is to cast the value to DECIMAL(5,2) but yes ROUND will certainly work.