目录
保留空值的 Split 方法:
代码:
SELECT REGEXP_SUBSTR(STR, '([^,]*)(,|$)', 1, LEVEL, NULL, 1) AS VAL
FROM (SELECT 'A,B,,D' AS STR FROM DUAL)
CONNECT BY LEVEL < REGEXP_COUNT(STR, ',') + 2;
执行结果:
VAL
1 A
2 B
3
4 D
原理:
前边几个参数大家应该都比较清楚,分别是:
source_char:要切分的字符串,必填
pattern:正则表达式,是实现匹配的逻辑,必填
position:开始匹配的位置,选填
occurrence:第几次出现的匹配,选填
match_param:模式
-
'i'
specifies case-insensitive matching, even if the determined collation of the condition is case-sensitive. -
'c'
specifies case-sensitive and accent-sensitive matching, even if the determined collation of the condition is case-insensitive or accent-insensitive. -
'n'
allows the period (.), which is the match-any-character character, to match the newline character. If you omit this parameter, then the period does not match the newline character. -
'm'
treats the source string as multiple lines. Oracle interprets the caret (^
) and dollar sign ($
) as the start and end, respectively, of any line anywhere in the source string, rather than only at the start or end of the entire source string. If you omit this parameter, then Oracle treats the source string as a single line. -
'x'
ignores whitespace characters. By default, whitespace characters match themselves.
我们关注一下这个 subexpr
这个参数,它用于pattern参数存在子表达式的 匹配操作中,表示匹配第几个子表达式,选填,为空是默认为全匹配,在上述例子中,如果我们不传入该参数,结果如下
VAL
1 A,
2 B,
3 ,
4 D
我们首先使用 ([^,]*)(,|$) 这样一个包含两个子表达式的 正则去匹配所有 使用 , 分隔的字串,包含匹配 , 本身。第一个子表达式用于匹配我们的目标字段,第二个子表达式用于匹配 , ,保证匹配上每一个分隔的字串,包含空字串。在获取结果时,我们使用 subexpr
参数,获取第一个子表达式上匹配出的内容。
无需保留空值的Split:
代码:
SELECT REGEXP_SUBSTR(STR, '[^,]+', 1, LEVEL) AS VAL
FROM (SELECT 'A,B,,D' AS STR FROM DUAL)
CONNECT BY LEVEL < REGEXP_COUNT(STR, ',') + 2;
执行结果:
VAL
1 A
2 B
3 D
4
相关函数说明:
1.CONNECT BY
CONNECT BY是一个常见的分层查询的 关键字
通过和 START WITH、PRIOT、LEVEL 等关键字的组合使用,可以对分层设计的数据结构进行树状查询。我们也可以利用对层级LEVEL的使用,生成数组,日历等操作,如下:
SELECT LEVEL, TRUNC(SYSDATE) + LEVEL
FROM DUAL
CONNECT BY LEVEL < 31;
2.REGEXP_COUNT
REGEXP_COUNT统计的匹配数量:
具体用法参照官方文档:https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/REGEXP_COUNT.html
还有
等函数