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);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;Details
The arguments must be convertible to a common type.