PostgreSQL: Select one of two fields depending on which is empty PostgreSQL: Select one of two fields depending on which is empty sql sql

PostgreSQL: Select one of two fields depending on which is empty


Use CASE WHEN .. THEN .. ELSE .. END, e.g.:

SELECT (CASE WHEN (field1 IS NULL OR field1 = '') THEN field2 ELSE field1 END)FROM table;

Check it out at the official docs.


Try COALESCE with NULLIF:

SELECT COALESCE(NULLIF(field1, ''), field2) AS the_field FROM my_table;


The ANSI SQL function Coalesce() is what you want.

 select Coalesce(field1,field2) from   table;