Update field only if it’s new value

Recently I wanted to find out how to update field only if the updated value would be new (as in – different from the current one).

After a bit of googling I managed to find the solution which I’m now presenting here for anyone that may need a solution for such an use case:

UPDATE table SET db_field=(CASE db_field WHEN NULL THEN 'new_value' ELSE db_field END);

 

Written by Nikola Brežnjak