SQL using If Not Null on a Concatenation

后端 未结 8 1761
孤独总比滥情好
孤独总比滥情好 2021-01-01 15:22

If I have the table

SELECT (Firstname || \'-\' || Middlename || \'-\' || Surname)  AS example_column
FROM example_table

This will

8条回答
  •  抹茶落季
    2021-01-01 15:40

    If you use Postgres, concat_ws() is what you are looking for:

    SELECT concat_ws('-', Firstname, Middlename, Surname)  AS example_column
    FROM example_table
    

    SQLFiddle: http://sqlfiddle.com/#!15/9eecb7db59d16c80417c72d1e1f4fbf1/8812

    To treat empty strings or strings that only contain spaces like NULL use nullif():

     SELECT concat_ws('-', Firstname, nullif(trim(Middlename), ''), Surname)  AS example_column
     FROM example_table
    

提交回复
热议问题