如何在oracle 11G中分隔逗号分隔值

时间:2014-12-17 20:32:52

标签: oracle oracle11g

我需要用逗号分隔列的值。

示例:BCDEY;我需要转换为B, C, D, E, Y。 按照"选择":

SELECT CDGRUPOCONDICAO FROM TBINTCLIENTE;

3 个答案:

答案 0 :(得分:2)

你也可以试试这个:

with cad as  
(select 'BCDEY' cad from dual)
select regexp_replace (regexp_replace(cad,'(.)','\1, '),', $','') cad_comma from cad;

答案 1 :(得分:1)

这样的事可能吗?

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通过将项目与', '加入来重新组合项目。

答案 2 :(得分:1)

另一个解决方案,使用recursive subquery factoring(因此,假设oracle&gt; = 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

请参阅http://sqlfiddle.com/#!4/d41d8/38971