For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!
Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.
with data as ( select '5,5,5,5,6,6,5,5,5,6,7,4,1,2,1,4,7,2' col from dual ) select listagg(col, ',') within group(order by 1) col from ( select distinct regexp_substr(col, '[^,]+', 1, level) col from data connect by level <= regexp_count(col, ',') ) COL ------------- 1,2,4,5,6,7
select distinct token from (SELECT TRIM( SUBSTR ( txt , INSTR (txt, ',', 1, level ) + 1 , INSTR (txt, ',', 1, level+1) - INSTR (txt, ',', 1, level) -1 ) ) AS token FROM ( SELECT ','||'5,5,5,5,6,6,5,5,5,6,7,4,1,2,1,4,7,2'||',' AS txt FROM dual ) CONNECT BY level <= LENGTH(txt)-LENGTH(REPLACE(txt,',',''))-1) order by 1
with data as ( select '5,5,5,5,6,6,5,5,5,6,7,4,1,2,1,4,7,2' col from dual ) select ltrim(max(sys_connect_by_path(col, ',')) keep (dense_rank last order by rn - 1), ',') col from ( select col, row_number() over (order by 1) rn from ( select distinct regexp_substr(col, '[^,]+', 1, level) col from data connect by level <= length(col) - length(replace(col, ',')) ) ) start with rn = 1 connect by prior rn = rn - 1; COL -------------- 1,2,4,5,6,7
with data as ( select '5,5,5,5,6,6,5,5,5,6,7,4,1,2,1,4,7,2' col from dual ) , r (s, c) as ( select cast(regexp_substr(col,'\d+') as varchar2(4000)) --alter the 4000 to your needs, the lower than lesser memory consumption ,replace(col ,regexp_substr(col,'\d+') ) from data union all select s||','||regexp_substr(c,'\d+') ,replace(c ,regexp_substr(c,'\d+') ) from r ) cycle c set is_cycle to 1 default 0 select s from r where trim(both ',' from c) is null and is_cycle = 0 S 5,6,7,4,1,2
with t as ( select '5,5,5,5,6,6,5,5,5,6,7,4,1,2,1,4,7,2' str from dual ) select x.newstr from t, xmltable( 'string-join(distinct-values(ora:tokenize($str,",")),",")' passing ',' || t.str as "str" columns newstr varchar2(2000) path '.' ) x / NEWSTR ------------ 1,2,4,5,6,7
create table my_order as ( select '5,5,5,5,6,6,5,5,5,6,7,4,1,2,1,4,7,2' id, cast (null as varchar2(4000)) cust_id from dual union all select '8,8,8,8,6,6,5,5,5,6,7,9,9,4,9,1,2,1,4,7,2', null from dual union all select '5714,5714,5714,5714,5714,5714,5714,5714,5714,5714,5714,5714,5668,5716,5714,5714,5714,5714,5714,5714,5714,5714,5714,5714,5714,5714,5668,5716', null from dual ) update my_order o set cust_id = ( with r (s, c, id) as ( select cast(regexp_substr(id,'\d+') as varchar2(4000)) ,replace(id ,regexp_substr(id,'\d+,?') ) ,id from my_order union all select s||','||regexp_substr(c,'\d+') ,replace(c ,regexp_substr(c,'\d+') ) ,id from r ) cycle c set is_cycle to 1 default 0 select s from r where trim(both ',' from c) is null and is_cycle = 0 and r.id = o.id ) select cust_id from my_order CUST_ID 5,6,7,4,1,2 8,6,5,7,9,4,1,2 5714,5668,5716
WITH INDATA AS ( select '9,9,8,8,7,7,6,6,5,5,4,4,3,3,2,2,1,1' x from dual union all select '9,9,8,8,7,7,4,4,3,3,2,2,1,1' x from dual ), TOKENIZED AS ( SELECT DISTINCT A.INNUM, dense_rank() over(partition by innum order by b.column_value) x_rank, B.COLUMN_VALUE x FROM (select rownum innum, x from indata) a, table(cast(multiset( SELECT trim(SUBSTR( x, (case level when 1 then 1 else instr(x, ',', 1, level-1) + 1 end), (case instr(x, ',', 1, level) when 0 then 4000 else instr(x, ',', 1, level) - 1 - (case level when 1 then 0 else instr(x, ',', 1, level-1) end) end) )) FROM dual CONNECT BY level <= LENGTH(x) - LENGTH(REPLACE(x, ',', '')) + 1 ) AS SYS.ODCIVARCHAR2LIST)) B ) SELECT ltrim(sys_connect_by_path(x, ','),',') x FROM TOKENIZED A where connect_by_isleaf = 1 START WITH X_RANK = 1 connect by (innum, x_rank) = ((prior innum, prior x_rank + 1)); X -------------------- 1,2,3,4,5,6,7,8,9 1,2,3,4,7,8,9
SQL> select * 2 from tbl 3 / CUST_ID ------------------------------------------------------------------------------------------------------------------------------------ 5,5,5,5,6,6,5,5,5,6,7,4,1,2,1,4,7,2 8,8,8,8,6,6,5,5,5,6,7,9,9,4,9,1,2,1,4,7,2 5714,5714,5714,5714,5714,5714,5714,5714,5714,5714,5714,5714,5668,5716,5714,5714,5714,5714,5714,5714,5714,5714,5714,5714,5714,5714,56 68,5716 SQL> select * 2 from v$version 3 / BANNER ---------------------------------------------------------------- Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bi PL/SQL Release 10.2.0.5.0 - Production CORE 10.2.0.5.0 Production TNS for 64-bit Windows: Version 10.2.0.5.0 - Production NLSRTL Version 10.2.0.5.0 - Production SQL> with t1 as ( select rowid rid, val from tbl, xmltable( '/a/b' passing xmltype('<a><b>' || replace(cust_id,',','</b><b>') || '</b></a>') columns val number path '.' ) ), t2 as ( select distinct rid, val from t1 ) select rtrim(xmlagg(xmlelement(e,val,',').extract('//text()')),',') new_cust_id from t2 group by rid / NEW_CUST_ID ---------------- 6,7,2,4,1,5 1,7,2,4,6,5,9,8 5714,5716,5668 SQL>
update my_order o set cust_id = ( with data as ( select ltrim(c,',') c ,id from my_order model partition by (id) dimension by (1 rn) measures (id i, cast(null as varchar2(4000)) c) rules iterate (1e6) until ltrim(i[1],',') is null ( c[1]=c[cv()]||','||regexp_substr(i[cv()],'\d+') ,i[1]=replace(i[cv()],regexp_substr(i[cv()],'\d+')) )) select c from data where id=o.id )
with data as ( select '5,5,5,5,6,6,5,5,5,6,7,4,1,2,1,4,7,2' col from dual union all select '5612,5613,5614,5613' col from dual union all select '1234,2345,3456,2222,2121,2121,1234' col from dual ) select ltrim(max(sys_connect_by_path(col, ',')) keep (dense_rank last order by rn - 1), ',') col from ( select col, row_number() over (partition by original_col order by 1) rn, original_col from ( select distinct regexp_substr(col, '[^,]+', 1, level) col, col original_col from data connect by level <= (length(col) - length(replace(col, ','))) and col = prior col and prior dbms_random.value is not null ) ) start with rn = 1 connect by prior rn = rn - 1 and original_col = prior original_col and prior dbms_random.value is not null group by original_col; COL -------------------------- 1234,3456,2345,2222,2121 7,6,2,4,1,5 5613,5612,5614
update my_order o set cust_id = ( with data as ( select ltrim(c,',') c ,id from (select distinct id from my_order) model partition by (row_number() over (order by id) r, id) dimension by (1 rn) measures (id i, cast(null as varchar2(4000)) c) rules iterate (1e6) until ltrim(i[1],',') is null ( c[1]=c[cv()]||','||regexp_substr(i[cv()],'\d+') ,i[1]=replace(i[cv()],regexp_substr(i[cv()],'\d+')) )) select c from data where id=o.id )