Issue with case statement

  • How to write 2 case statements in single update statement.

    update table
    set col1 = case when id is null and country = 'USA' and ID2 like '%-%'
                    then loc1
                else 'AB'
                 end,
        col1 = case when country = 'USA'
               then NULL
    end
    from table

    Its giving error like cannot col1 is specified more than once in set clause

  • I think maybe you just need multiple WHEN clauses in the CASE, instead of multiple CASE statements.  maybe something like:

    update table
    set col1 =
        case
        when id is null and country = 'USA' and ID2 like '%-%' then loc1
        when country = 'USA' then NULL
        else 'AB'
        end

    or are these really supposed to be 2 different columns in your destination table?

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

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