使用 regexp_substr 按顺序拆分 Oracle 中的字符串

2021-12-30 00:00:00 sql oracle11g oracle

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

I have a string in Oracle database, my string is: 'bbb;aaa;qqq;ccc'

我使用正则表达式来分割我的字符串:

I used regexp for split my string:

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 ;

我想按顺序拆分它,我希望总是以下输出:

I want to split it in order, I expected the below output always:

bbb
aaa
qqq
ccc

因为 subString 的顺序对我来说非常重要.但是这个查询的结果不是有序的:

because order of the subString are very important for me. but the result of this query is not in order:

qqq
aaa
bbb
ccc

推荐答案

您不需要 DISTINCT 即可获得结果;此外,要以给定的顺序获得结果,您只需要一个 ORDER BY 子句:

You don't need a DISTINCT to get your result; besides, to get the result in a given order, all you need is an ORDER BY clause:

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

相关文章