gpt4 book ai didi

sql - 使用 regexp_substr 在 Oracle 中按空格和字符作为分隔符拆分字符串

转载 作者:行者123 更新时间:2023-12-04 14:07:42 25 4
gpt4 key购买 nike

我正在尝试使用 regexp_subtr 拆分字符串,但我无法使其工作。

所以,首先,我有这个查询

select regexp_substr('Helloworld - test!' ,'[[:space:]]-[[:space:]]') from dual

这很好地提取了我的分隔符 - 空白空白

但是,当我尝试使用此选项拆分字符串时,它不起作用。
select regexp_substr('Helloworld - test!' ,'[^[[:space:]]-[[:space:]]]+')from dual

查询不返回任何内容。

帮助将不胜感激!
谢谢

最佳答案

SQL Fiddle

Oracle 11g R2 架构设置 :

CREATE TABLE TEST( str ) AS
SELECT 'Hello world - test-test! - test' FROM DUAL
UNION ALL SELECT 'Hello world2 - test2 - test-test2' FROM DUAL;

查询 1 :
SELECT Str,
COLUMN_VALUE AS Occurrence,
REGEXP_SUBSTR( str ,'(.*?)([[:space:]]-[[:space:]]|$)', 1, COLUMN_VALUE, NULL, 1 ) AS split_value
FROM TEST,
TABLE(
CAST(
MULTISET(
SELECT LEVEL
FROM DUAL
CONNECT BY LEVEL < REGEXP_COUNT( str ,'(.*?)([[:space:]]-[[:space:]]|$)' )
)
AS SYS.ODCINUMBERLIST
)
)

Results :
|                               STR | OCCURRENCE |  SPLIT_VALUE |
|-----------------------------------|------------|--------------|
| Hello world - test-test! - test | 1 | Hello world |
| Hello world - test-test! - test | 2 | test-test! |
| Hello world - test-test! - test | 3 | test |
| Hello world2 - test2 - test-test2 | 1 | Hello world2 |
| Hello world2 - test2 - test-test2 | 2 | test2 |
| Hello world2 - test2 - test-test2 | 3 | test-test2 |

关于sql - 使用 regexp_substr 在 Oracle 中按空格和字符作为分隔符拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31654411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com