使用SQL用单个逗号替换字符串中的多个逗号 [英] Use SQL to Replace Multiple Commas in a String with a Single Comma

查看:429
本文介绍了使用SQL用单个逗号替换字符串中的多个逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入:你好,你好吗?好的,谢谢,,,,,,詹姆斯·雅顿.

我想用一个逗号和一个空格替换所有连续的逗号.

I would like to replace all consecutive commas with a single comma and a space.

输出应为:

你好吗?很好,谢谢,詹姆斯,雅顿.

SELECT REGEXP_REPLACE('Hi,,How are you? Fine, thanks ,, ,  ,,,, , James,Arden.', ',,+', ', ') FROM DUAL;

我还没有测试它,因为我还没有访问Oracle系统的权限.

I haven't tested it yet as I don't have access to the Oracle system yet.

推荐答案

具有相同输出的更简单的解决方案:

More simple solution with same output:

Hi, How are you? Fine, thanks, James, Arden.

@Joseph B:对不起,我无法发表评论!所以我在这里发布答案.

@Joseph B: Sorry I can not comment yet! So I post my answer here.

SELECT 
        REGEXP_REPLACE(
            REGEXP_REPLACE('Hi,,How are you? Fine, thanks ,, ,  ,,,, , James,Arden.', ', | ,', ','), --Replace ', ' and ' ,' with ','
        ',{1,}', ', ') single_comma_text --Replace one or more comma with comma followed by space
FROM DUAL;

您可以检查 SQLFiddle

这篇关于使用SQL用单个逗号替换字符串中的多个逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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