我想获得两个分号之间的值。Oracle正则表达式子字符串
select (REGEXP_SUBSTR(col,'[^:]+',1,2,null)) from test
这是我处理的行:
1236:10:EXEC
1236::EXEC
在我的结果集,我想:
10
<null>
以上返回查询:
10
EXEC
我想获得两个分号之间的值。Oracle正则表达式子字符串
select (REGEXP_SUBSTR(col,'[^:]+',1,2,null)) from test
这是我处理的行:
1236:10:EXEC
1236::EXEC
在我的结果集,我想:
10
<null>
以上返回查询:
10
EXEC
你可以避免使用正则表达式;例如:
select substr(col, instr(col, ':') +1, instr(col, ':', 1, 2) - instr(col, ':') -1)
from test
工作原理:
select instr(col, ':') as firstColon,
instr(col, ':', 1, 2) as secondColon,
instr(col, ':', 1, 2) - instr(col, ':') -1 as lengthOfResultingString,
substr(col, instr(col, ':') +1, instr(col, ':', 1, 2) - instr(col, ':') -1) as result
from test
给出:
FIRSTCOLON SECONDCOLON LENGTHOFRESULTINGSTRING RESULT
---------- ----------- ----------------------- ------------
5 8 2 10
5 6 0
这是假设你总是在字符串中的至少两个冒号。
用正则表达式,在慢,但更紧凑的方式,你可以使用:
regexp_substr(col, ':([^:]*):', 1, 1, null, 1)
感谢。那太棒了 – user1060187
with test (col) as
(
select '1236:10:EX' from dual union all
select '1543::Ex' from dual
)
select regexp_replace(col, '(:(.*):)|.', '\2') from test;
这样使用REGEXP_SUBSTR处理NULL列表元素。第四个参数是你想要的列表元素:
with test (col) as
(
select '1236:10:EX' from dual union all
select '1543::Ex' from dual
)
select regexp_substr(col, '(.*?)(:|$)', 1, 2, NULL, 1) from test;
使用'REGEXP_SUBSTR(S, ':([^:] *)',1,1,NULL,1)' –