Coalesce and Concat in the same statement, Postgre

2019-02-27 09:04发布

问题:

I am trying to concat an individual's first and last name together but coalesce a team name when there is a null value. Unfortunately my syntax is returning a SPACE, so coalesce does not recognize it as a null value.. What can I do to correct this?

Syntax I am currently using:

coalesce((Concat(first_name,' ',last_name)),team_name)

回答1:

Just use the concatenation operator, ||:

coalesce(first_name || ' ' || last_name, team_name)

The concat() function ignores NULL values. The operator returns NULL.