I am running a query in Postgres using the ||
operator to concatenate some columns and it seems where one of the columns is blank the whole result is blank. Is that normal ||
behaviour?
With "blank" I mean NULL
.
operatorpostgresql
I am running a query in Postgres using the ||
operator to concatenate some columns and it seems where one of the columns is blank the whole result is blank. Is that normal ||
behaviour?
With "blank" I mean NULL
.
Best Answer
Depends what "blank" means,
NULL
or the empty string.If it means
NULL
, then yes, that's normal.SELECT 'ab' || NULL ;
will return null.If it means an empty string, then no.
SELECT 'ab' || '' ;
will return'ab'
.To avoid the issue, you can convert the nulls to empty string with
COALESCE()
:or use
CONCAT()
orCONCAT_WS()
functions - which ignore nulls: