ozzboy
ozzboy

Reputation: 2720

UPDATE column based on 2 other Columns Oracle

My table has 5 columns. I would like to

UPDATE Column5='TEACHER' IF Column2='Y' 
AND 
UPDATE Column5='STUDENT' IF Column3='Y' 

Can I do this in a single statement?

Upvotes: 1

Views: 1475

Answers (1)

Justin Cave
Justin Cave

Reputation: 231851

You can probably use a CASE statement. My guess is that you want something like this

UPDATE table_name
   SET column5 = (CASE WHEN column2 = 'Y'
                       THEN 'TEACHER'
                       WHEN column3 = 'Y'
                       THEN 'STUDENT'
                       ELSE null
                   END)
 WHERE column2 = 'Y'
    OR column3 = 'Y'

I'm guessing that you don't want to update rows where neither condition is met. If you want to update every row, you'll want to remove the WHERE clause and you may want to adjust what the default value is (the ELSE in the CASE) for rows where neither condition is met.

Upvotes: 5

Related Questions