Wednesday, 13 January 2016

Synthesizing rows in oracle

Purpose:  Convert a comma delimited string into rows. In following example i am using the string '123,456,789',

SELECT SUBSTR(('123,456,789'),
              loc + 1,
              NVL(LEAD(loc) OVER(ORDER BY loc) - loc - 1,
                  length('123,456,789') - loc)) output
  FROM (SELECT DISTINCT INSTR(('123,456,789'), ',', 1, LEVEL) loc
          FROM dual
        CONNECT BY LEVEL < LENGTH('123,456,789')) ;
OUTPUT
-----------
123
456
789