Operand data type varchar is invalid for sum operator - Pretty please, can someone help?

  • Hi There!

    I have created a simple table with 3 fields "Con_Flag" , "ZL_Count" and "Total_Op_Atts".

    "Con_Flag" has been created with a CASE Statement (populated with 1 OR 0).

    I would like to populate "Con_Flag" with figures from table1

    I would like to SUM on "ZL_Count" (as I will later be doing a calculation between "ZL_Count" and "Total_OP_Att's")

    CREATE TABLE #ZL_Calculation1

    (Con_Flag INT,

    ZL_Count INT,

    Total_Op_Atts INT)

    INSERT #ZL_Calculation1

    (Con_Flag,ZL_Count)

    SELECT Con_Flag,

    SUM (ZL_Count)

    FROM [#ZL_TBL1]

    GROUP BY Con_Flag

    For some reason I get the above message. Not quite sure why; haven't I defined the column correctly?

    Any help would be appreciated! 🙂

    My desired outcome would be:

    Con_Flag ZL_CountTotal_OP_ATT

    0 500

    1 3752

  • What is the table definition of #ZL_TBL1?

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • assuming all the values are numeric, you should explicitly convert it to int inside the sum:

    SUM (CONVERT(int,ZL_Count))

    alternatively, the better solution is to make sure your destination table has that column defined as integer, instead of however it gets created now.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply