简体   繁体   中英

T-SQL cannot convert varcchar column to a numeric datatype

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 ie "5449.0______________" filled up with spaces.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM