Click here to Skip to main content
15,904,155 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Friends

I need to take Sum(some varchar values) from my Table Its var char thats why I tried to cast that values first like this


(Select SUM((convert(int,RunTime))) from #TempReport)

or
SELECT SUM(CAST(RunTime AS int)) from #TempReport



Then also its showing some conversion errors : Problem is that my varchar field contain some values like 2:30,5:20,6:10 like this so how to remove the ':' this symbol and take sum?

Please give me some ideas...
Posted
Comments
Saral S Stalin 30-Mar-12 5:28am    
You want to change 2:30 to 230 or 2, 30?
Tony Tom.k 30-Mar-12 5:35am    
I need result in 0:00 format
Saral S Stalin 30-Mar-12 5:36am    
Did not get that Bro. What would be the expected sum of 2:30, 5:20, 6:10?
Tony Tom.k 30-Mar-12 5:38am    
I need to generate some report in excel So the Sum look like this 13:60

1 solution

Here you go

SQL
DECLARE @TempReport TABLE (RunTIMe VARCHAR(20))
INSERT @TempReport VALUES ('2:30')
INSERT @TempReport VALUES ('5:20')
INSERT @TempReport VALUES ('6:10')

SELECT  SUM(CAST(REPLACE(RunTime, ':','.') AS float)) from @TempReport


If '.' has to replaced to ':' cast to varchar again and do a replace
 
Share this answer
 
Comments
Tony Tom.k 30-Mar-12 5:59am    
Thank You Bro Its Working Fine
Saral S Stalin 30-Mar-12 6:01am    
Happy to help..If it answeres your question mark it as answer
member60 30-Mar-12 6:06am    
My 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900