Often developers have to split the string concatenated with "," (comma).
v_str := 'a,b,c,d,e,f,g,h';
Generally developers use combination of SUBSTR and INSTR to get each value.
Did you know Oracle had a in-built package which would do this for us.
DBMS_UTILITY.comma_to_table is the procedure, which would split the string for us.
No need to loop over to get the next index position etc..
DECLARE
PROCEDURE PARSE(string_in IN varchar2)
is
l_array DBMS_UTILITY.uncl_array;
l_count PLS_INTEGER;
BEGIN
DBMS_UTILITY.comma_to_table(string_in,l_count,l_array);
for indx in 1..l_count
loop
DBMS_OUTPUT.put_line(l_array(indx));
end loop;
exception
when others then
DBMS_OUTPUT.put_line(sqlcode || 'Bad List !');
end;
begin
parse('a,b,c');
end;
I guess this works only for strings, not for integers. Ex: 1,2,3,4 - does this work? Can you check?
ReplyDeleteYes it does'nt work for numbers, In that case use below:
ReplyDeletewith data
as
(
select
trim( substr(txt,
instr(txt, ',', 1, level) + 1,
instr(txt, ',', 1, level+1)
- instr(txt, ',', 1, level) -1 ) )
as token
from (select ','||:txt||',' txt
from dual)
connect by level <=
length(:txt)-length(replace(:txt,',',''))+1
)
select * from data;
--By Tom Kyte