comparing two fields that may be null

yes, there is, but it is recomended to not use it. here is sample:

t=# select null = null;
 ?column?
----------

(1 row)

t=# set transform_null_equals = on;
SET
t=# select null = null;
 ?column?
----------
 t
(1 row)

UPDATE: apparently would work only for comparison column = NULL, not column = column:

t=# with s as (select null::int a, null::int b) select a <> b from s;
 ?column?
----------

(1 row)

so the shortest comparison would be coalesce:

t=# with s as (select null::int a, null::int b) select coalesce(a,b,0) = 0 from s;
 ?column?
----------
 t
(1 row)

yes, there is IS DISTINCT FROM and IS NOT DISTINCT FROM

postgres=# \pset null ****
Null display is "****".
postgres=# select null = null;
┌──────────┐
│ ?column? │
╞══════════╡
│ ****     │
└──────────┘
(1 row)

postgres=# select null is not distinct from null;
┌──────────┐
│ ?column? │
╞══════════╡
│ t        │
└──────────┘
(1 row)

postgres=# select 10 = null;
┌──────────┐
│ ?column? │
╞══════════╡
│ ****     │
└──────────┘
(1 row)

postgres=# select 10 is distinct from null;
┌──────────┐
│ ?column? │
╞══════════╡
│ t        │
└──────────┘
(1 row)

postgres=# select 10 is not distinct from null;
┌──────────┐
│ ?column? │
╞══════════╡
│ f        │
└──────────┘
(1 row)

postgres=# select 10 is not distinct from 20;
┌──────────┐
│ ?column? │
╞══════════╡
│ f        │
└──────────┘
(1 row)

Tags:

Postgresql