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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 how to calculate time part

Author  Topic 

vish2success
Starting Member

1 Post

Posted - 2010-01-28 : 09:30:27
I HAVE STRING COLUMN SAY "TIME_USED" WITH 'HOURS:MINS:SECONDS' (eg 153:29:41) FORMAT

I NEED TO CALCULATE IT TYPICALLY AND STORE IT IN SOME OTHER VARIABLE PLEASE CHECK;

--> (MINS.SECONDS)/60 ie (29.41)/60=0.4901
and concatinate this value to HOURS ie 153.4901


how to do this, i cant use like operator and get the data and do in one go for sql, can any one help me out

Regards
Naren

DP978
Constraint Violating Yak Guru

269 Posts

Posted - 2010-01-28 : 09:46:57
Just as an FYI 29:41 != 29.41. If you want decimal form of that you need to do ( 29 + (41/60) ) / 60

2 things people will tell you...

1. Your data better be consistent.
2. Try to avoid formatting Datetimes as anything BUT datetimes, they act funky, sort wrong, and typically produce bad answers.

Also if you must do what you want you are going to need a few functions...

1st: CharIndex to find the first ':'
2nd: Reverse & CharIndex to find the 2nd ':'
3rd: Cast to change the numbers between the ':' to integers and also to change the trailing digits to integers
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-01-28 : 10:09:40
Why do you want to do this?




declare @time varchar(10)
set @time='153:29:41'
select substring(@time,1,charindex(':',@time)-1)+'.'+cast(replace(substring(@time,charindex(':',@time)+1,len(@time)),':','.') as decimal(12,2))/60




Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -