docs:postgres:concatenating_null_strings

concatenating null strings

In Postgres, when using the || operator to concatenate fields into one string, if any of the fields contain a null value then the result will entirely be a null value. To avoid this problem, we can use the coalesce function:

SELECT 'Doe' || ', ' || NULL || 'John' AS name
-- result will be null
 
SELECT COALESCE('Doe','') || COALESCE(', ','') || COALESCE(NULL,'') || COALESCE('John','') AS name
-- result will be 'Doe, John'
  • docs/postgres/concatenating_null_strings.txt
  • Last modified: 2008/08/03 00:25
  • by 127.0.0.1