Amlan Roshan
Amlan Roshan

Reputation: 1

Update table with conditions in Oracle

I just came across a scenario i.e there is table BATCH with 10 records with column sl_no and batch_typ like below.

Table Batch

Here I want to update the Batch_typ where BLUE it should be green and vice versa with SQL query.Thanks in advance.

Upvotes: 0

Views: 44

Answers (3)

Vishweshwar
Vishweshwar

Reputation: 56

You can use update with Case for this kind of scenarios

UPDATE BATCH set batch_typ =
case when batch_typ = 'BLUE' then 'GREEN'
     when batch_typ = 'GREEN' then 'BLUE' end;

````````````````

Upvotes: 0

Jim Macaulay
Jim Macaulay

Reputation: 5141

Please use below update statement,

update BATCH set batch_typ =
case when batch_typ = 'BLUE' then 'GREEN'
     when batch_typ = 'GREEN' then 'BLUE' end;

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520958

You may update using a CASE expression:

UPDATE BATCH
SET BATCH_TYP = CASE WHEN BATCH_TYP = 'GREEN' THEN 'BLUE' ELSE 'GREEN' END
WHERE BATCH_TYP IN ('GREEN', 'BLUE');

Upvotes: 1

Related Questions