Search code examples
tokenizestring-parsingoracle11gr2varchar2

Parse varchar2 to table (Oracle)


Is there built-in function in Oracle DB 11g r2 that could parse varchar2 variable to table? Opposite of listagg or wm_concat. I found only Tom Kyte's method dated 2006:

with 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;

I think Oracle must have simpler way.


Solution

  • No.

    I would simplify Tom's method slightly, but not by much; you can now use regular expressions as well:

    select regexp_substr(:txt, '[^,]+', 1, level)
       from dual
     connect by regexp_substr(:txt, '[^,]+', 1, level) is not null
    

    SQL Fiddle