我想獲得兩個分號之間的值。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)' –