PostgreSQL – Comparing Two Values When One or More Could Be NULL

nullpostgresql

I am writing a trigger and want to see if one column has changed could be NULL and trying to avoid the following:

IF     NEW.value != OLD.value 
    OR (NEW.value IS NULL AND OLD.value IS NOT NULL) 
    OR (NEW.value IS NOT NULL AND OLD.value IS NULL) THEN
...
END IF;

This is postgres 9.5 in a function (stored procedure)

Best Answer

Equivalent and simpler:

IF  NEW.value IS DISTINCT FROM OLD.value  THEN
...
END IF;