【问题标题】:How to get the strings between the given string in oracle如何在oracle中获取给定字符串之间的字符串
【发布时间】:2020-02-06 18:48:30
【问题描述】:

这就是我的数据的样子。

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中得到这个?

【问题讨论】:

    标签: sql oracle substring


    【解决方案1】:

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

    Oracle 设置

    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
    ~~~2019-09-30 14:34:11@@@MF_APX_PLM_Promo1.msgflow***MF_APX_PLM_Promo2.msgflow

    db小提琴here

    【讨论】:

      【解决方案2】:

      使用 REGEXP_SUBSTR

      https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=0981e7232b873934c5d4e833c141f47a

      with tbl 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' str 
      from dual)
      select TRIM( '^' FROM REGEXP_SUBSTR(str, '.+?\^{3}')),
      TRIM( '^' FROM REGEXP_SUBSTR(str, '\^{3}.+?\^{3}',1,1,'n'))  from tbl
      

      输出

      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
      

      【讨论】:

        【解决方案3】:

        当窗口弹出时复制你的字符串或替换为 :str

        select 
        substr(:str,
        instr(:str,'^^^',1,1) +3,
        instr(:str,'^^^',1,2) - instr(:str,'^^^',1,1) - 3)
        from dual;
        

        【讨论】:

          【解决方案4】:

          我将以不同的方式处理它,并假设您正准备解析这个多分隔字符串。由于第一级分隔似乎是'^^^',让我们使用connect by 遍历字符串并拆分这些元素,只返回前2 个,因为这是您的要求。这将返回由 '^^^' 分隔的前 2 个元素:

          with tbl(str) 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' str 
            from dual
          )
          select regexp_substr(str, '(.*?)(\^\^\^|$)', 1, level, NULL, 1) element
          from tbl
          connect by level <= 2; --regexp_count(str, '\^\^\^')+1;
          

          请注意,如果您只需要特定元素,请将 regexp_substr 调用中的“级别”替换为元素的编号。

          【讨论】:

            猜你喜欢
            • 2017-12-23
            • 1970-01-01
            • 2017-12-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-01-29
            相关资源
            最近更新 更多