How to concat_ws multiple fields and remove duplicate separators for empty slots

后端 未结 1 679
天命终不由人
天命终不由人 2020-12-30 04:40

When you CONCAT_WS(\' \',field1,field2,field3) in MySQL and if one of the fields is empty, not null, you get multiple separators.

An example can be:

相关标签:
1条回答
  • 2020-12-30 05:23

    Do it like this:

    CONCAT_WS(' ', NULLIF(field1, ''), NULLIF(field2, ''), NULLIF(field3, ''));
    

    CONCAT_WS will skip any null values, and by using NULLIF any empty ones too.

    Note: You can't regex replace. MySQL does not support it.

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