Q:

How to update specific field only if its previous value is not null, out of multiple fields

I'm updating a, b, c, d values along with where condition where id being compared. I need to update field 'b' only if its previous value is not null.
I need to update remaining fields though, In the same query.
my current query is like 
update demo_table set a='str1', b='str2', c=112, d='str3' where id=191;

can anyone suggest a way to achieve this. Thanks in adavance.postgre

A:

You can just add a CASE condition to your SET clause:
UPDATE demo_table
SET
  a = 'str1',
  b = CASE b IS NOT NULL WHEN true THEN 'str2' ELSE NULL END,
  c = 112,
  d = 'str3'
WHERE id = 191;

