Search code examples
sqlsql-serversql-server-2008t-sql

Varchar invalid for Sum operator


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

I wrote a query like:

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

But i am getting this error:

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

Can anyone help me?


Solution

  • Try doing this:

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