How to separate comma separated values ββin oracle 11G
3 answers
Something like this maybe?
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)
)
Production:
B, C, D, E, Y
Here the subquery splits the string character with a character. The outer then listagg
reassembles the elements by attaching them to ', '
.
+1
source to share
Another solution using recursive subquery factoring (hence assuming oracle> = 11g release 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)
Production:
LISTAGG(C,',')WITHINGROUP(ORDERBYLVL)
B, C, D, E, Y
A, B, C
+1
source to share