2

我要从破折号 (-) 前后的字符串中提取字符。到目前为止,我能够使用下面的代码返回破折号之前的所有数字:

SELECT 
INSTR('100-7', '-'),
SUBSTR('100-7', 1, INSTR('100-7', '-')-1)
FROM dual;

现在我正在尝试找到一种方法来返回破折号后的所有字符。所以如果我有一个字符串:

20-150

查询应该只返回 150。无论如何我可以做到这一点吗?顺便说一句,我正在使用 Oracle。多谢你们 :)

4

4 回答 4

7

这将在破折号后返回所有内容

SELECT SUBSTR(value, INSTR(value, '-')+1) invalue
  FROM (SELECT '20-150' value FROM dual) t;

输出:

| INVALUE |
-----------
|     150 |

sqlfiddle

并将两者放在一起

SELECT SUBSTR(value, 1, INSTR(value, '-') - 1) lvalue, 
       SUBSTR(value, INSTR(value, '-') + 1) rvalue
  FROM (SELECT '20-150' value FROM dual) t;

输出:

| LVALUE | RVALUE |
-------------------
|     20 |    150 |

sqlfiddle

于 2013-03-04T06:49:54.283 回答
1

使用REGEXP_REPLACE

SELECT REGEXP_REPLACE('20-150','(.*)-','') 
FROM dual;
于 2013-03-04T06:52:26.053 回答
0

使用 REGEXP_SUBSTR :

SELECT REGEXP_SUBSTR ('20-15055555555', '[^-]+', 1, 2) rep
  FROM DUAL;

你可以试试这个

于 2013-03-04T11:46:52.993 回答
-1

查询应该是这样的。

select substr('P_A_VA1234_1_EE',(INSTR('P_A_VA1234_1_EE','_',1,2)+1),(INSTR('P_A_VA1234_1_EE','_',1,4)-(INSTR('P_A_VA1234_1_EE','_',1,2)+1)))original from dual

select substr(shipment_gid,(INSTR(shipment_gid,'_',1,2)+1),(INSTR(shipment_gid,'_',1,4)-(INSTR(shipment_gid,'_',1,2)+1)))original from dual

SUBSTR(S.SHIPMENT_GID,8,8) LOADID,

select substr(shipment_gid,(INSTR(shipment_gid,'_',1,2)+1),(INSTR(shipment_gid,'_',1,4)-(INSTR(shipment_gid,'_',1,2)+1)))load,shipment_gid from shipment where shipment_gid like '%DHL/CLS.CLS_CV_%';

SUBSTR(S.SHIPMENT_GID,(INSTR(S.SHIPMENT_GID,'_',1,2)+1),(INSTR(S.SHIPMENT_GID,'_',1,4)-(INSTR(S.SHIPMENT_GID,'_',1,2)+1)))
于 2020-08-13T08:35:09.220 回答