docs:postgres:concatenating_null_strings

This is an old revision of the document!


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 coallesce 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.1207341288.txt.gz
  • Last modified: 2008/08/03 00:25
  • (external edit)