Sql 按顺序使用regexp\u substr拆分Oracle中的字符串

Sql 按顺序使用regexp\u substr拆分Oracle中的字符串,sql,oracle,oracle11g,Sql,Oracle,Oracle11g,我在Oracle数据库中有一个字符串,我的字符串是:'bbb;aaa;qqq;ccc' 我使用regexp分割字符串: select distinct trim(regexp_substr('bbb;aaa;qqq;ccc','[^;]+', 1,level) ) as q from dual connect by regexp_substr('bbb;aaa;qqq;ccc', '[^;]+', 1, level) is not null ; 我想将其按顺序拆分,我希望始终有以下输出: b

我在Oracle数据库中有一个字符串,我的字符串是:'bbb;aaa;qqq;ccc'

我使用regexp分割字符串:

select distinct trim(regexp_substr('bbb;aaa;qqq;ccc','[^;]+', 1,level) ) as q 
from dual
connect by regexp_substr('bbb;aaa;qqq;ccc', '[^;]+', 1, level) is not null ;
我想将其按顺序拆分,我希望始终有以下输出:

bbb
aaa
qqq
ccc
因为子字符串的顺序对我来说非常重要。但此查询的结果不符合顺序:

qqq
aaa
bbb
ccc

您不需要使用
不同的
来获得结果;此外,要以给定的顺序获得结果,您只需要一个
orderby
子句:

select trim(regexp_substr('bbb;aaa;qqq;ccc','[^;]+', 1,level) ) as q 
from dual
connect by regexp_substr('bbb;aaa;qqq;ccc', '[^;]+', 1, level) is not null
order by level

您不需要使用
不同的
来获得结果;此外,要以给定的顺序获得结果,您只需要一个
orderby
子句:

select trim(regexp_substr('bbb;aaa;qqq;ccc','[^;]+', 1,level) ) as q 
from dual
connect by regexp_substr('bbb;aaa;qqq;ccc', '[^;]+', 1, level) is not null
order by level

如果确实需要
区分

WITH your_data( value ) AS (
  SELECT 'bbb;aaa;qqq;ccc;aaa;eee' FROM DUAL
),
positions ( string, lvl, start_pos, end_pos ) AS (
  SELECT value, 1, 1, INSTR( value, ';', 1, 1 ) FROM your_data
UNION ALL
  SELECT string, lvl + 1, end_pos + 1, INSTR( string, ';', 1, lvl + 1 )
  FROM positions
  WHERE  end_pos > 0
),
substrings ( string, substring, lvl, start_pos ) AS (
  SELECT string,
         DECODE( end_pos, 0, SUBSTR( string, start_pos ), SUBSTR( string, start_pos, end_pos - start_pos ) ),
         lvl,
         start_pos
  FROM   positions
)
SELECT string,
       substring,
       lvl
FROM   substrings
WHERE  INSTR( ';' || string || ';', ';' || substring || ';' ) = start_pos;
输出

STRING                  SUBSTRING                      LVL
----------------------- ----------------------- ----------
bbb;aaa;qqq;ccc;aaa;eee bbb                              1
bbb;aaa;qqq;ccc;aaa;eee aaa                              2
bbb;aaa;qqq;ccc;aaa;eee qqq                              3
bbb;aaa;qqq;ccc;aaa;eee ccc                              4
bbb;aaa;qqq;ccc;aaa;eee eee                              6

如果确实需要
区分

WITH your_data( value ) AS (
  SELECT 'bbb;aaa;qqq;ccc;aaa;eee' FROM DUAL
),
positions ( string, lvl, start_pos, end_pos ) AS (
  SELECT value, 1, 1, INSTR( value, ';', 1, 1 ) FROM your_data
UNION ALL
  SELECT string, lvl + 1, end_pos + 1, INSTR( string, ';', 1, lvl + 1 )
  FROM positions
  WHERE  end_pos > 0
),
substrings ( string, substring, lvl, start_pos ) AS (
  SELECT string,
         DECODE( end_pos, 0, SUBSTR( string, start_pos ), SUBSTR( string, start_pos, end_pos - start_pos ) ),
         lvl,
         start_pos
  FROM   positions
)
SELECT string,
       substring,
       lvl
FROM   substrings
WHERE  INSTR( ';' || string || ';', ';' || substring || ';' ) = start_pos;
输出

STRING                  SUBSTRING                      LVL
----------------------- ----------------------- ----------
bbb;aaa;qqq;ccc;aaa;eee bbb                              1
bbb;aaa;qqq;ccc;aaa;eee aaa                              2
bbb;aaa;qqq;ccc;aaa;eee qqq                              3
bbb;aaa;qqq;ccc;aaa;eee ccc                              4
bbb;aaa;qqq;ccc;aaa;eee eee                              6