SQL Coalesce Statement

November 5, 2012

Coalesce returns the first non null parameter. You can specify both columns and static strings as parameters, and you can specify as many parameters as you need to.

SELECT b.name, COALESCE(r.country_code, 'unknown')
FROM bands_band b
INNER JOIN regions_region r ON r.id = b.region_id

In the results of this query, the second column will contain unknown where the country_code that would have been returned is null.