Tal
Tal

Reputation: 355

Update specific values in more than one column - SQL

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.

Upvotes: 0

Views: 81

Answers (2)

Giorgi Nakeuri
Giorgi Nakeuri

Reputation: 35780

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'

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269583

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.

Upvotes: 2

Related Questions