Function Reference

COALESCE

Replaces NULL with the first value from the list that is not NULL.

PostgreSQL 17.5
COALESCE(value[, ...])

Parameters

value
Values to check, separated by commas

Return value

The first non-NULL value; NULL if all arguments are NULL

Examples

First non-NULL value

PostgreSQL 17.5
SELECT COALESCE(NULL, NULL, 1, 2);
coalesce
1

Replacing a missing middle name

PostgreSQL 17.5
SELECT last_name,
	first_name,
	COALESCE(middle_name, 'not specified') AS middle_name
FROM Teacher
WHERE id BETWEEN 9 AND 12;
last_namefirst_namemiddle_name
OstrozhskayaViktoriyaNikolaevna
KrylovYUrijnot specified
EvseevAndrejnot specified
MoiseevBogdanRomanovich

Details

The arguments must be convertible to a common type.

See also

Where to learn