BIGuy BIGuy - 1 month ago 10
SQL Question

SQL - Operand data type varchar is invalid for sum operator

I'm running the following query and getting the Operand data type varchar is invalid for sum operator error. The accountnature field is a varchar field, but I'm casting it as INT and I'm still getting the error.

SELECT
'Qualified' =sum(case when (cast(AccountNature AS Int)) in ('a','b') then '1' end)

from agreement

group by accountnature


Any thoughts on what I'm doing wrong or how to fix?
Thanks.

Answer

Try This:

SELECT
    'Qualified' = sum(case when AccountNature in ('a','b') then 1 ELSE 0 end)
from agreement
group by accountnature
Comments