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?
LVL 1
HLRosenbergerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

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

Mike
0
SStoryCommented:
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.
0
HLRosenbergerAuthor Commented:
yes, I was looking to do this using SQL.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Brian CroweDatabase AdministratorCommented:
DECLARE @Time      VARCHAR(10) = '1:20';

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SStoryCommented:
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.
0
HLRosenbergerAuthor Commented:
No problem.  I inadvertently included vb.net tag.
0
HLRosenbergerAuthor Commented:
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?
0
HLRosenbergerAuthor Commented:
ROUND()  works for rounding.  Thanks!@
0
Brian CroweDatabase AdministratorCommented:
Another option is to cast the value to DECIMAL(5,2) but yes ROUND will certainly work.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.