1

我试图在两个不同的单词之间提取数字,但是“ 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 
4

2 回答 2

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* 匹配零个或多个数字。
于 2013-08-10T01:35:07.370 回答
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
于 2013-08-10T11:16:38.193 回答