Update specific values in more than one column - SQL

92 Views Asked by At

I have a table that contains two columns, both have email values. I want to create a query that update a specific data in both columns.

For example if I have two records of the email '[email protected]' in one column and three records of '[email protected]' in the other column I want them both to be updated.
Here for example I want that all the '[email protected]' will be '[email protected]':

enter image description here

My question is how the query should look like.

2

There are 2 best solutions below

2
On

The simplest way is to run two update statements:

update table
    set col1 = <newval>
    where col1 = <oldval>;

update table
    set col2 = <newval>
    where col2 = <oldval>;

This begs of the question of why two columns are storing the same data. Perhaps you need to review your data structure and use a junction table for this information.

0
On

If you insist of one statement:

update table
     set col1 = iif(col1 = 'aa', 'bb', col1),
         col2 = iif(col2 = 'aa', 'bb', col2)
where col1 = 'aa' or col2 = 'aa'