【发布时间】:2016-05-18 11:50:12
【问题描述】:
我需要将几个字段连接在一起,这些字段可能为空,也可能不为空。我可能最终得到一个字符串,例如:',,c,,e,,' 我实际上想显示为 'c,e'。
我可以通过regexp_replace 和trim 的组合来获得这个:
with sd as (select 'a,b,c' str from dual union all
select 'a' str from dual union all
select null str from dual union all
select 'a,,,d' from dual union all
select 'a,,,,e,f,,'from dual union all
select ',,,d,,f,g,,'from dual)
select str,
regexp_replace(str, '(,)+', '\1') new_str,
trim(both ',' from regexp_replace(str, '(,)+', '\1')) trimmed_new_str
from sd;
STR NEW_STR TRIMMED_NEW_STR
----------- ----------- ---------------
a,b,c a,b,c a,b,c
a a a
a,,,d a,d a,d
a,,,,e,f,, a,e,f, a,e,f
,,,d,,f,g,, ,d,f,g, d,f,g
但我觉得它应该在单个 regexp_replace 中是可行的,只是我一生都无法弄清楚如何做到这一点!
有可能吗?如果有,怎么做?
【问题讨论】:
标签: sql oracle regexp-replace