在多个分隔符上拆分字符串

时间:2016-10-07 18:46:50

标签: sql oracle

我有一个sql,它返回下面的示例字符串:

输入 PKIND:BCMOX:10048301-

输出 BCMOX:10048301

我需要在-上的字符串的第一个子字符串中编写代码,然后将其拆分为:并返回2& 3项(BCMOX:10048301

2 个答案:

答案 0 :(得分:2)

如果字符串格式一致,并且您希望在第一次:之后提取所有内容,直到第一次出现-,请使用substrinstr的组合。

select substr(col, instr(col,':')+1, instr(col,'-')-instr(col,':')-1)
from yourtable
where instr(col,':') > 0 and instr(col,'-') > 0 --to get the rows which have these 2 characters

答案 1 :(得分:0)

REGEXP_SUBSTR版本。返回第一个冒号和第一个连字符之间的所有内容。

select regexp_substr('PKIND:BCMOX:10048301-', ':(.*)-', 1, 1, NULL, 1) from dual;