如何使用逗号分隔的字符串和逗号之间的(可能)空白字段按顺序拆分字符串

时间:2019-04-04 07:21:42

标签: regex oracle plsql

我需要在Oracle PL / SQL中以逗号分隔的字符串顺序拆分,包括空字段作为“无”值。 这是一个包含6个字段的示例,但可以包含更多或更少的字段

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, '[^;]+'));

输出:

 value1      
 value2      
 value5      
 value6      
 none         
 none     

理想的输出:

value1      
value2  
none      
none  
value5      
value6      

1 个答案:

答案 0 :(得分: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.