SQL using If Not Null on a Concatenation

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

If I have the table

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

This will

8条回答
  •  萌比男神i
    2021-01-01 15:56

    This may be a viable option:

    SELECT FirstName || '-' || ISNULL(MiddleName + '-', '') || Surname
    

    Since a NULL concatenated with a string yields a NULL, we can attempt to build our sub-string and replace a NULL with an empty string, which is then concatenated to the next part of the name.

    This assumes that FirstName and Surname are always NOT NULL, but you could apply the same logic to then as well.

提交回复
热议问题