Need to split a string into rows. Empty values should be separate rows in the same order.
value: ',,hello,,,world,,'
1 ->
2 ->
3 -> hello
4 ->
5 ->
6 -> world
7 ->
8 ->
I tried following query as suggested in various threads.
WITH
qstr AS (select ',,,,1,2,3,4,,' str from dual)
SELECT level ||'->'||
REGEXP_SUBSTR(str, '[^,]+', 1, LEVEL) value FROM qstr
CONNECT BY LEVEL <= REGEXP_COUNT(str, ',') + 1
It moves all empty values down in the list of rows and nonempty to the top. I would like to maintain the order as well.
1->hello
2->world
3->
4->
5->
6->
7->
8->
For now I am using a hack to insert empty string before each empty value delimiter which is working. I was wondering if there any efficient way of doing it.
WITH
qstr AS (select replace(',,,,1,2,3,4,,',',',' ,') str from dual)
SELECT level ||'->'||
REGEXP_SUBSTR(str, '[^,]+', 1, LEVEL) value FROM qstr
CONNECT BY LEVEL <= REGEXP_COUNT(str, ',') + 1
Result:
1->
2->
3->hello
4->
5->
6->world
7->
8->