在单词之间提取文本

时间:2013-08-09 20:01:02

标签: oracle

我尝试在两个单词之间提取数字,但“substr”不能与“instr”结合使用。至少我没有得到!

示例:

CODTAX  TAX_DESCRIPTION
-----------------------
1225    NCM 85444900 IPI 0% ICMS 18% CST 060 IVA 41,00% IVA EXT 51,32%

“IPI”与“%ICMS”之间的提取数 0 或“IVA”与“%IVA”之间的提取数 41.00

CREATE TABLE TAX
(
  CODTAX           NUMBER,
  TAX_DESCRIPTION  VARCHAR2(200 BYTE)
);

INSERT INTO TAX (CODTAX, TAX_DESCRIPTION)
     VALUES (1505, 'NCM 29051220 IPI 0% ICMS 18% CST 000 IVA 0% IVA EXT 0%');
INSERT INTO TAX  (CODTAX, TAX_DESCRIPTION) 
     VALUES (1258, 'NCM 85444900 IPI 0% ICMS 18% CST 060 IVA 41,00% IVA EXT 51,32%');
INSERT INTO TAX (CODTAX, TAX_DESCRIPTION)
     VALUES (2051, 'NCM 85362000 IPI 10% ICMS 18% CST 060 IVA 43,00% IVA EXT 53,46%');

结果必须是:

CODTAX  TAX_DESCRIPTION                                                 IPI    IVA
----------------------------------------------------------------------------------
1505    NCM 29051220 IPI 0% ICMS 18% CST 000 IVA 0% IVA EXT 0%            0      0 
1258    NCM 85444900 IPI 0% ICMS 18% CST 060 IVA 41,00% IVA EXT 51,32%    0  41,00 
2051    NCM 85362000 IPI 10% ICMS 18% CST 060 IVA 43,00% IVA EXT 53,46%  10  43,00 

2 个答案:

答案 0 :(得分:0)

您可以尝试INSTR和REGEXP_SUBSTR的组合。

SELECT A.*,
       REGEXP_SUBSTR (A.TAX_DESCRIPTION,
                      '\d+,?\d*',
                      INSTR (a.tax_description, 'IPI')) IPI,
       REGEXP_SUBSTR (A.TAX_DESCRIPTION,
                      '\d+,?\d*',
                      INSTR (a.tax_description, 'IVA')) IVA
  FROM TAX A;

INSTR用于查找IPI和IVA的第一个位置。然后在REGEXP_SUBSTR函数中使用它来指定起始位置。

正则表达式,

  • \ d +匹配一个或多个数字。
  • ,?匹配零或一个逗号。
  • \ d *匹配零个或多个数字。

答案 1 :(得分:0)

substrinstr变体:

SQLFiddle

select
  codtax, 
  tax_description, 
  (
    trim(substr(
      tax_description,
      instr(tax_description, 'NCM') + length('NCM'),
      instr(tax_description, 'IPI') - 
        (instr(tax_description, 'NCM') + length('NCM'))
    ))
  ) ncm,
  (
    trim(substr(
      tax_description,
      instr(tax_description, 'IPI') + length('IPI'),
      instr(tax_description, '% ICMS') - 
        (instr(tax_description, 'IPI') + length('IPI'))
    ))
  ) ipi,
  (
    trim(substr(
      tax_description,
      instr(tax_description, 'ICMS') + length('ICMS'),
      instr(tax_description, '% CST') - 
        (instr(tax_description, 'ICMS') + length('ICMS'))
    ))
  ) icms,
  (
    trim(substr(
      tax_description,
      instr(tax_description, 'CST') + length('CST'),
      instr(tax_description, 'IVA') - 
        (instr(tax_description, 'CST') + length('CST'))
    ))
  ) cst,
  (
    trim(substr(
      tax_description,
      instr(tax_description, 'IVA') + length('IVA'),
      instr(tax_description, '% IVA EXT') - 
        (instr(tax_description, 'IVA') + length('IVA'))
    ))
  ) iva,
  (
    trim(substr(
      tax_description,
      instr(tax_description, 'IVA EXT') + length('IVA EXT'),
      instr(tax_description, '%', -1) - 
        (instr(tax_description, 'IVA EXT') + length('IVA EXT'))
    ))
  ) iva_ext
from tax