首页 > 解决方案 > 如何在oracle中获取给定字符串之间的字符串

问题描述

这就是我的数据的样子。

APP_APX_PLM_PostCategory~~~pavan anand~~~2019-09-26 15:03:39@@@MF_APX_PLM_PostBuyProgram.msgflow***MF_APX_PLM_PostBuyProgram1.msgflow***MF_APX_PLM_PostBuyProgram2.msgflow^^^APP_APX_PLM_Promo~~~skola2
~~~2019-09-30 14:34:11@@@MF_APX_PLM_Promo1.msgflow***MF_APX_PLM_Promo2.msgflow^^^APP_APX_PLM_Santosh~~~skola2~~~2019-09-30 14:39:26@@@MF_PKMS_DA_TransferPickInboundPreProcessor.msgflow***MF_PKMS_DA_TransferPickInboundPreProcessor.msgflow

我想获取从第一个字符到第一次出现 ^^^ 的数据,以及从第一个 ^^^ 到第二个 ^^^ 的数据。

谁能帮我在oracle中得到这个?

标签: sqloraclesubstring

解决方案


用于INSTR查找分隔符的位置,然后SUBSTR提取子字符串:

甲骨文设置

CREATE TABLE test_data ( value ) AS
  SELECT 'APP_APX_PLM_PostCategory~~~pavan anand~~~2019-09-26 15:03:39@@@MF_APX_PLM_PostBuyProgram.msgflow***MF_APX_PLM_PostBuyProgram1.msgflow***MF_APX_PLM_PostBuyProgram2.msgflow^^^APP_APX_PLM_Promo~~~skola2
~~~2019-09-30 14:34:11@@@MF_APX_PLM_Promo1.msgflow***MF_APX_PLM_Promo2.msgflow^^^APP_APX_PLM_Santosh~~~skola2~~~2019-09-30 14:39:26@@@MF_PKMS_DA_TransferPickInboundPreProcessor.msgflow***MF_PKMS_DA_TransferPickInboundPreProcessor.msgflow' FROM DUAL;

查询

SELECT SUBSTR( value, 1, first_position - 1 ) AS first_substr,
       SUBSTR( value, first_position + 3, second_position - first_position - 3 ) AS second_substr
FROM   (
  SELECT value,
         INSTR( value, '^^^', 1, 1 ) AS first_position,
         INSTR( value, '^^^', 1, 2 ) AS second_position
  FROM   test_data
)

输出

FIRST_SUBSTR | SECOND_SUBSTR                                                                                               
:------------------------------------------------ -------------------------------------------------- -------------------------------------------------- -------------------- | :------------------------------------------------ -------------------------------------------------- --------
APP_APX_PLM_PostCategory~~~pavan anand~~~2019-09-26 15:03:39@@@MF_APX_PLM_PostBuyProgram.msgflow***MF_APX_PLM_PostBuyProgram1.msgflow***MF_APX_PLM_PostBuyProgram2.msgflow | APP_APX_PLM_Promo~~~skola2<br>~~~2019-09-30 14:34:11@@@MF_APX_PLM_Promo1.msgflow***MF_APX_PLM_Promo2.msgflow

db<>在这里摆弄


推荐阅读