返回文本中子字符串的订单号

时间:2014-03-19 19:37:52

标签: sql oracle

我需要解析字符串,但我无法识别订单号。

这里有一些预期答案的例子。我需要Oracle SQL表达式来讽刺价值

SOURCE_COLUMN      PARAMETER         RETURN_VALUE
AAA_BBB_CCC_DDD    AAA               1
AAA_BBB_CCCC_DDD   BBB               2
AAA_BBB_CC_DDD     CC                3
AAA_BBBB_CCC_DDD   DDD               4
AAA_BBB_CCC_DDD    EEE               0

这是用于生成前两列的SQL

select 'AAA_BBB_CCC_DDD' SOURCE_COLUMN, 'AAA' PARAM FROM DUAL UNION ALL
select 'AAA_BBB_CCCC_DDD' SOURCE_COLUMN, 'BBB' PARAM FROM DUAL UNION ALL
select 'AAA_BBB_CC_DDD' SOURCE_COLUMN, 'CC' PARAM FROM DUAL UNION ALL
select 'AAA_BBBB_CCC_DDD' SOURCE_COLUMN, 'DDD' PARAM FROM DUAL UNION ALL
select 'AAA_BBB_CCC_DDD' SOURCE_COLUMN, 'EEE' PARAM FROM DUAL

3 个答案:

答案 0 :(得分:2)

此查询可以执行您想要的操作:

select (case when source_column like '%'||param||'%'
             then 1 +
                  coalesce(length(substr(source_column, 1, instr(source_column, param) - 1)) -
                           length(replace(substr(source_column, 1, instr(source_column, param) - 1), '_', '')),
                           0)
              else 0
        end) as pos
from t;

这个想法比查询看起来简单得多。它找到匹配的参数,然后将初始子字符串带到该点。您可以使用技巧计算'_'的数量:获取字符串的长度,然后在用'_'替换''时减去字符串的长度。您想要的值实际上比该值多一个。并且,如果找不到模式,则返回0

答案 1 :(得分:0)

对于您的特定示例(稳定字符串模式):

SQL> with t as (
  2  select 'AAA_BBB_CCC_DDD' SOURCE_COLUMN, 'AAA' PARAM FROM DUAL UNION ALL
  3  select 'AAA_BBB_CCC_DDD' SOURCE_COLUMN, 'BBB' PARAM FROM DUAL UNION ALL
  4  select 'AAA_BBB_CCC_DDD' SOURCE_COLUMN, 'CCC' PARAM FROM DUAL UNION ALL
  5  select 'AAA_BBB_CCC_DDD' SOURCE_COLUMN, 'DDD' PARAM FROM DUAL UNION ALL
  6  select 'AAA_BBB_CCC_DDD' SOURCE_COLUMN, 'EEE' PARAM FROM DUAL
  7  )
  8  select SOURCE_COLUMN, PARAM, floor((instr(SOURCE_COLUMN,param)+3)/4) p  from t;

SOURCE_COLUMN   PAR          P                                                  
--------------- --- ----------                                                  
AAA_BBB_CCC_DDD AAA          1                                                  
AAA_BBB_CCC_DDD BBB          2                                                  
AAA_BBB_CCC_DDD CCC          3                                                  
AAA_BBB_CCC_DDD DDD          4                                                  
AAA_BBB_CCC_DDD EEE          0  

答案 2 :(得分:0)

您可以按以下方式使用ROW_NUMBER()

SELECT source_column
        ,PARAM
        ,CASE WHEN ind IS NULL THEN 0 
         ELSE Row_number() over ( PARTITION BY source_column ORDER BY ind) END AS Return_Value 
FROM   (SELECT source_column
                ,Param
                ,CASE WHEN Instr(source_column, param) = 0 THEN NULL ELSE Instr(source_column, param) END AS Ind 
        FROM   (select 'AAA_BBB_CC_DDD' SOURCE_COLUMN, 'AAA' Param FROM DUAL UNION ALL
                select 'AAA_BBB_CC_DDD' SOURCE_COLUMN, 'BBB' Param FROM DUAL UNION ALL
                select 'AAA_BBB_CC_DDD' SOURCE_COLUMN, 'CC' Param FROM DUAL UNION ALL
                select 'AAA_BBB_CC_DDD' SOURCE_COLUMN, 'DDD' Param FROM DUAL UNION ALL
                select 'AAA_BBB_CC_DDD' SOURCE_COLUMN, 'EEE' Param FROM DUAL
                )
        ) 

这里SQL小提琴显示结果http://sqlfiddle.com/#!4/d41d8/26754