【发布时间】:2014-12-19 17:31:43
【问题描述】:
我需要用逗号分隔列的值。
示例:BCDEY;我需要转换为B, C, D, E, Y。
跟随“选择”:
SELECT CDGRUPOCONDICAO FROM TBINTCLIENTE;
【问题讨论】:
我需要用逗号分隔列的值。
示例:BCDEY;我需要转换为B, C, D, E, Y。
跟随“选择”:
SELECT CDGRUPOCONDICAO FROM TBINTCLIENTE;
【问题讨论】:
你也可以试试这个:
with cad as
(select 'BCDEY' cad from dual)
select regexp_replace (regexp_replace(cad,'(.)','\1, '),', $','') cad_comma from cad;
【讨论】:
可能是这样的吗?
with testdata as (select 'BCDEY' str from dual)
select listagg(c, ', ') within group(order by lvl)
from (
select substr(str, level, 1) c,
level lvl
from testdata
connect by level <= length(str)
)
制作:
B, C, D, E, Y
这里,子查询逐个字符地分割字符串。然后外部listagg 重新组装项目,将它们与', ' 连接起来。
【讨论】:
另一种解决方案,使用recursive subquery factoring(因此,假设 oracle >= 11g 第 2 版):
with testdata as (select 1 id, 'BCDEY' str from dual union all
select 2, 'ABC' from dual),
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-- replace that subquery by your actual query
splited(c,r,id,lvl) as (
select '' c, str r, id, 0 lvl from testdata
union all
select substr(r, 1, 1) c,
substr(r, 2) r,
id,
lvl+1
from splited
where r is not null)
select listagg(c, ', ') within group(order by lvl)
from splited
group by (id)
制作:
LISTAGG(C,',')WITHINGROUP(ORDERBYLVL)
B, C, D, E, Y
A, B, C
【讨论】: