How to Use Coalesce

The coalesce function returns the first non null parameter, is a sort of isnull function. Using the previous table test, we can do:

WITH test AS (
   SELECT 'John' as first_name, 'John' as last_name UNION ALL
   SELECT 'John', 'Peter' UNION ALL
   SELECT 'John', NULL
)
select last_name, coalesce(last_name , 'xxxxxx') as coalesce from test;
last_name coalesce
John John
Peter Peter
null xxxxxx

IN THIS PAGE