Skip to content
Advertisement

How do I update two variables in a oracle sql server update using case statement

I am trying to update two variables in a Oracle server as follows:

UPDATE UserTable
SET user_email='asdf@company.com', 
               (CASE WHEN reason != '' THEN why_update= 'change email server' END)
WHERE user_id = 123

I want to update why_update column only if the user provided a reason for update otherwise leave the column as it is (which is varchar type and can be NULL).

Or any other better solution?

Advertisement

Answer

THis will work:

UPDATE UserTable
SET user_email='asdf@company.com', 
    why_update= CASE WHEN reason != '' THEN 'change email server' else why_update END
WHERE user_id = 123
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement