0

i have the following T-SQL statement:

select top 10 value1 from table1 where condition1 = 12345

result:

5449.0              
228231.0            
0.0                 
3128.0              
6560.0              
4541.0              
2119.0              
0.0                 
0.0                 
4183.0              

the data type of value1 = "[char] (20) COLLATE Latin1_General_CS_AS NULL"

please note, each result line has 20 chars i.e. "5449.0______________" filled up with spaces.

I want to sum all this columns. how can i convert these values to a summable data type?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sep Pei
  • 21
  • 2
  • possible duplicate of [Convert a string to int using sql query](http://stackoverflow.com/questions/728833/convert-a-string-to-int-using-sql-query) – Evan Trimboli Mar 03 '15 at 09:51

1 Answers1

0

Use cast or convert:

-- for demo
declare  @v char(20)
set @v = '228231.9            '
-- real magic
select cast(@v as real)

So this is the select in your case:

select cast(value1 as real) from table1 where condition1 = 12345
select sum(cast(value1 as real)) from table1 where condition1 = 12345
Laszlo T
  • 1,165
  • 10
  • 22