September 7, 2012 at 3:55 am
I have a feild in a table of type varchar that contains time as
hhh:mm:ss, ss and mm is upto 59 but hhh is unrestricted
e.g 73:59:59 or 144:44:59
What I want is to get time in hours, can anyone help me write a query???
September 7, 2012 at 4:11 am
SELECT
TimeVC,
FirstColonPosition = CHARINDEX(':',TimeVC,1),
[Hours] = LEFT(TimeVC,CHARINDEX(':',TimeVC,1)-1)
FROM (
SELECT TimeVC = '73:59:59' UNION ALL
SELECT '144:44:59') d
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
September 7, 2012 at 5:34 am
Thanks cris, you helped me solve my issue
and I will definately follow forum's rules next time π
Thanks alot π
September 7, 2012 at 5:35 am
Borrowing on Chris' code, here's one way to convert the time to decimal hours.
SELECT TimeVC,
[Hours] = LEFT(TimeVC,CHARINDEX(':',TimeVC,1)-1)
+ CONVERT(FLOAT,CONVERT(DATETIME,'00'+SUBSTRING(TimeVC,CHARINDEX(':',TimeVC,1),8000)))*24
FROM (
SELECT TimeVC = '73:59:59' UNION ALL
SELECT '144:44:59'
) d
Results:
TimeVC Hours
--------- ----------------------
73:59:59 73.9997222222222
144:44:59 144.749722222222
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply