Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UPDATE if column is not null, if null then leave at null

I have the following SQL:

UPDATE TableA
SET first_name = 'AAA',
    last_name = 'BBB',
    address1 = '123',
    address2 = 'Fake St.,',
    phone = '1234567',
    id = '11223344'

What should I use to only update each column if it is not null?

like image 943
k1f1 Avatar asked Aug 27 '12 09:08

k1f1


1 Answers

update tableA
set first_name = case when first_name is null then null else 'aaa' end,
last_name = case when last_name is null then null else 'bbb' end,
...
like image 176
podiluska Avatar answered Oct 23 '22 11:10

podiluska