Skip to content
Advertisement

SQL IF in column THEN replace with the adjacent cell

I have a table with different values (alphanumeric) and an other one with translations (also alphanumeric). I want to search values from the first table if they are in a specific column of the other table. If yes, then replace with a value corresponding the row.

Example :

TABLE 1

**Type_of_products**
Desk
Fauteuil
Bureau
Chair
Laptop
Car

TABLE 2

**English**    **French**
Desk       Bureau
Chair      Fauteuil
Laptop     Ordinateur
Car        Voiture

FINAL TABLE

**Type_of_products**
Desk
Chair
Desk
Chair
Laptop
Car

So what I want is :

IF “from table1.type_of_products” IS IN {“table2.french”}

THEN replace by corresponding value in “table2.english”

Advertisement

Answer

You can use left join:

select t1.*, coalesce(t2.english, t1.type) as english_version
from table1 t1 left join
     table2 t2
     on t1.type = t2.french;
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement