将值从字符转换为货币时出错

2021-09-10 00:00:00 tsql sql-server

我有一个表 tableA,其中包含数据类型 varchar 的列 [Amount].

I have a table tableA that contains a column [Amount] of datatype varchar.

它具有如下值:

47980.89
333652.61
332388.84
374664.48
368715.26
371689.33
371689.33
368715.26
374664.48 

现在,当我运行查询时,它运行成功,但每次都给出不同的输出

Now when I run my query, it runs successfully but gives different output each time

SELECT  
   sum(convert(float, Amount))
FROM tableA   

当我尝试其他语句时,出现错误

and when I try this other statement, I get an error

无法将字符值转换为货币.char 值的语法不正确.

Cannot convert a char value to money. The char value has incorrect syntax.

代码:

SELECT  
    sum(convert(money, Amount))
FROM tableA

我想要列的总和 [Amount]

推荐答案

应该这样做:

select Sum(isnull(cast(amount as float),0)) from #t

或者你需要它是货币数据类型

or you need it to be money data-type

select Sum(isnull(cast(amount as money),0)) from #t

相关文章