How to concatenate columns in a Postgres SELECT?

后端 未结 8 1644
醉酒成梦
醉酒成梦 2020-11-27 13:31

I have two string columns a and b in a table foo.

select a, b from foo returns values a and b<

相关标签:
8条回答
  • 2020-11-27 13:52

    The problem was in nulls in the values; then the concatenation does not work with nulls. The solution is as follows:

    SELECT coalesce(a, '') || coalesce(b, '') FROM foo;
    
    0 讨论(0)
  • 2020-11-27 13:58

    it is better to use CONCAT function in PostgreSQL for concatenation

    eg : select CONCAT(first_name,last_name) from person where pid = 136

    if you are using column_a || ' ' || column_b for concatenation for 2 column , if any of the value in column_a or column_b is null query will return null value. which may not be preferred in all cases.. so instead of this

    ||

    use

    CONCAT

    it will return relevant value if either of them have value

    0 讨论(0)
提交回复
热议问题