Home Forums SQL Server 2012 SQL Server 2012 - T-SQL how to update column city value from 'A' to 'B' and 'B' to 'A' in single query RE: how to update column city value from 'A' to 'B' and 'B' to 'A' in single query

  • DECLARE @t TABLE(city VARCHAR(1))

    INSERT INTO @t(city)

    VALUES ('A'),( 'B'),( 'C'),( 'A'),( 'B'),( 'C'),( 'A');

    -- do a select and not the results for comparison purposes

    SELECT city FROM @t

    UPDATE @t

    SET city = CASE city

    WHEN 'A' THEN 'B'

    WHEN 'B' THEN 'A'

    else city

    END

    -- select after update to compare with initial select

    select * from @t

    SQL 2000/2005/2008/2012 DBA - MCTS/MCITP