July 10, 2013

SQL Question


Suppose in a table we have 1 column
now column1 has values 
a
b
a
b
a
b

Table name : Table1
Column Name : Column1

Question : update table1 where 'a' should be replaced with 'b'  and 'a' should be replaced with 'd'

2 comments:

  1. SET @value1 = 'b'
    SET @value2 = 'a'
    UPDATE table1 SET val = REPLACE(@value1 + @value2,val,'')
    select REPLACE(@value1 + @value2,val,'') from table1

    ReplyDelete
  2. Even simple way to update the value as follows :

    update table1
    set Column1 =
    Case when 'a' then 'b'
    When 'b' then 'a'
    end

    ReplyDelete