Appearance
COALESCE
Return the value of the first expression in a list of one or more expressions that is not null. If all expressions are null, return null. As soon as a non-null value is found, do not evaluate any remaining expressions. ISNULL
is an equivalent conditional expression, but it only accepts two arguments.
COALESCE( expression [, ...] )
For example, if the city
column value is null, return No City
:
premdb=# SELECT teamid, COALESCE(city,'No City') FROM team ORDER BY teamid DESC;
teamid | coalesce
--------+---------------
50 | No City
49 | No City
48 | No City
47 | Wolverhampton
46 | London
45 | Wigan
44 | London
...
Make sure the list of expressions consists of compatible data types. For example, you cannot reference a SMALLINT and a VARCHAR in a COALESCE expression:
premdb=# select name,nickname,city,coalesce(teamid,name) from team order by teamid;
ERROR: COALESCE types smallint and character varying cannot be matched
LINE 1: select name,nickname,city,coalesce(teamid,name) from team or...