Filling gaps with COALESCE
In a report, a blank or NULL cell can be confusing. COALESCE replaces NULL with a value of your
choice:
SELECT name, COALESCE(phone, 'No phone') AS phone
FROM customersCOALESCE takes a list of values and returns the first one that isn't NULL. Here, if phone has
a value, you get the phone number; if it's NULL, you get 'No phone'.
You can give more than two options. This uses the phone number, then the email address, then a fixed message:
COALESCE(phone, email, 'No contact details')Use a matching type
The replacement should be the same kind of value as the column: text for a text column, a number
for a number column. COALESCE(credit_limit, 0) works; COALESCE(credit_limit, 'None') fails,
because 'None' isn't a number.