Skip to content

NULLIF

Return a null value if value1 equals value2; otherwise return value1.

NULLIF(value1, value2)

For example:

premdb=# \pset null null
Null display is "null".

premdb=# premdb=# SELECT DISTINCT m.htid, h.htid, nullif(m.htid,h.htid) 
FROM match m JOIN hometeam h ON m.htid=h.htid ORDER BY m.htid LIMIT 5; 
 htid | htid | nullif 
------+------+--------
   2 |    2 |   null
   3 |    3 |   null
   4 |    4 |   null
   5 |    5 |   null
   6 |    6 |   null
(5 rows)
premdb=# SELECT teamid, name, city, NULLIF(name,city) 
FROM team ORDER BY 1;
 teamid |          name           |     city      |         nullif          
--------+-------------------------+---------------+-------------------------
     1 | Arsenal                 | London        | Arsenal
     2 | Aston Villa             | Birmingham    | Aston Villa
     3 | Barnsley                | Barnsley      | null
     4 | Birmingham City         | Birmingham    | Birmingham City
     5 | Blackburn Rovers        | Blackburn     | Blackburn Rovers
     6 | Blackpool               | Blackpool     | null
     7 | Bolton Wanderers        | Bolton        | Bolton Wanderers
     8 | Bournemouth             | Bournemouth   | null
     9 | Bradford City           | Bradford      | Bradford City
    10 | Burnley                 | Burnley       | null
    11 | Cardiff City            | Cardiff       | Cardiff City
...