本文介绍了如何在SQL Server中采用Sum(varchar)值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Friends

我需要从我的表中获取Sum(一些varchar值).

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




然后它还显示了一些转换错误:问题是我的varchar字段包含像这样的2:30,5:20,6:10之类的一些值,因此如何删除符号:"并取和?

请给我一些想法...




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...

推荐答案

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



如果必须将.''替换为:",则再次将其转换为varchar并进行替换



If ''.'' has to replaced to '':'' cast to varchar again and do a replace


这篇关于如何在SQL Server中采用Sum(varchar)值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 11:04