开发者

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

开发者 https://www.devze.com 2023-01-19 16:54 出处:网络
When you CONCAT_WS(\' \',field1,field2,field3) in MySQL and if one of the fields is empty, not null, you get multiple separators. 开发者_开发技巧

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:

John[space][space][space]Doe[space]III.

How can I make sure there is only one separator.


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

精彩评论

暂无评论...
验证码 换一张
取 消