Salva
Salva

Reputation: 113

How to combine column using case condition in oracle sql

I have a below table

id name addr 1 addr 2 EXTRA
1 aaa US RED
1 bbb IN IN YELLOW
1 ccc US RED
1 ccc IN IN ORANGE
1 ccc EU GREEN

as depicted in above table, u can see that i want to bring IN value of EXTRA col into addr2 column overriding the value in addr2. i want to apply case condition or decode to achieve this.

id name addr 1 addr 2
1 aaa US RED
1 bbb IN YELLOW
1 ccc US RED
1 ccc IN ORANGE
1 ccc EU GREEN

thanks in advance.

Upvotes: 0

Views: 294

Answers (2)

Rahid Zeynalov
Rahid Zeynalov

Reputation: 187

If you want to do this on your select statement you can use following one:

select  id
        , name
        , addr1
        , case when EXTRA IS NULL THEN addr2 ELSE EXTRA END as addr2
from    tablename
;

If you want to change date in table then you can use:

update tablename
set addr2 = EXTRA
where EXTRA is not null;

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269693

If I understand correctly, you can use coalesce():

select t.*,
       coalesce(extra, addr2) as new_addr2
from t;

If you want to change the data in the table, then use update:

update t
    set addr = extra
    where extra is not null;

Upvotes: 1

Related Questions