Varchar 对 Sum 运算符无效

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

我有一个名为 Cos 的表,Amt 的数据类型是 Float,示例数据如下所示:

I have a table called Cos and the datatype of Amt is Float and sample data looks like:

Acct  Period  F_year   Amt
Detf  1       2011     Null
Detf  2       2011     Null
Detf  3       2011     1669.57
FTE   1       2011     3205.11
FTE   2       2011     0
FTE   3       2011     Null

我写了一个查询,如:

Select Acct,Period,F_year, Sum(AMT) as Amt
from dbo.Cos
Group By Acct,Period,F_year
Where Amt is not null

但我收到此错误:

Msg 8117, Level 16, State 1, Line 1
Operand data type varchar is invalid for sum operator.

有人可以帮我吗?

推荐答案

尝试这样做:

Select Acct,Period,F_year, Sum(isnull(cast(AMT as float),0)) as Amt
from dbo.Cos
Group By Acct,Period,F_year

相关文章