Splitting Comma Separated string into columns by using REGEXP_SUBSTR

oracleoracle-11g

Here is my example string:

select 'po1,qty1,po2,qty2,..,POn,QTYn' str from dual.

The string length is dynamic. It may have more than one PO and QTY.

Now, I want the output displayed as all the POs in first column and quantities in
second column.

Best Answer

select
  regexp_substr(pq, '[^,]+', 1, 1) as po,
  regexp_substr(pq, '[^,]+', 1, 2) as qty
from
(
  select
    regexp_substr('po1,qty1,po2,qty2','[^,]+,[^,]+', 1, level) as pq
  from dual
    connect by regexp_substr('po1,qty1,po2,qty2','[^,]+,[^,]+', 1, level) is not null
);


PO         QTY
---------- ----------
po1        qty1
po2        qty2