如何concat_ws多个字段并删除空插槽的重复分隔符 [英] How to concat_ws multiple fields and remove duplicate separators for empty slots

查看:196
本文介绍了如何concat_ws多个字段并删除空插槽的重复分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MySQL中CONCAT_WS(' ',field1,field2,field3)且如果字段之一为空而不是null时,将获得多个分隔符.

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

一个例子可以是:

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

如何确保只有一个分隔符.

How can I make sure there is only one separator.

推荐答案

这样做:

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

CONCAT_WS将跳过任何空值,也将通过使用NULLIF任何空值.

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

注意:您不能用正则表达式替换. MySQL不支持它.

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

这篇关于如何concat_ws多个字段并删除空插槽的重复分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆