0

I need to split a string in Oracle PL/SQL with comma-separated in order, including empty fields as "none" value. This is an example with 6 fields but can have more or less fields

line varchar2(100) := "value1;value2;;;value5;value6;";
WITH test AS (SELECT line FROM DUAL)
  SELECT NVL(REGEXP_SUBSTR (line, '[^;]+', 1, ROWNUM),'none') as SPLIT
    FROM test
  CONNECT BY LEVEL <= LENGTH (REGEXP_REPLACE (line, '[^;]+'));

Output:

 value1      
 value2      
 value5      
 value6      
 none         
 none     

Desirable output:

value1      
value2  
none      
none  
value5      
value6      
8
  • Did you try [^;]*(?=;)? Commented Apr 4, 2019 at 7:41
  • Hey, I tried your change, but the output is all "none"s. Commented Apr 4, 2019 at 7:47
  • Refer to this answer stackoverflow.com/a/35462641/7998591 Commented Apr 4, 2019 at 7:59
  • Solutions from the duplicates: dbfiddle.uk/… Commented Apr 4, 2019 at 8:05
  • @horcrux Oracle does not support look-ahead/behind in regular expressions. Commented Apr 4, 2019 at 8:06

1 Answer 1

3
with test as (select 'value1;value2;;;value5;value6;' line from dual)
select nvl(regexp_substr (line, '([^;]*);', 1, rownum, null, 1), 'none') as split
from test
connect by level <= regexp_count (line, ';');

SPLIT                         
------------------------------
value1
value2
none
none
value5
value6

6 rows selected. 
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect answer. Thanks!!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.