Oracle正则表达式子字符串在给定字符串中添加特定数字

时间:2017-03-10 11:26:57

标签: sql oracle

Oracle正则表达式substring,用于在给定字符串中添加特定数字

8030/N/25.00,8017/N/25.00,8089/N/50.00

我上面有字符串,我需要从上面的字符串中添加8030 + 8017 + 8089

如何在oracle SQL或pl / SQL中解决它。

3 个答案:

答案 0 :(得分:0)

结合使用数学,SUBSTRINSTR& REVERSE实现这一目标。

文字字符串:

SELECT SUBSTR('8030/N/25.00,8017/N/25.00,8089/N/50.00',1,4) AS a,
SUBSTR(substr('8030/N/25.00,8017/N/25.00,8089/N/50.00', - instr(reverse('8030/N/25.00,8017/N/25.00,8089/N/50.00'), '/N/',1,2) - 6),1,4) AS b,
SUBSTR(substr('8030/N/25.00,8017/N/25.00,8089/N/50.00', - instr(reverse('8030/N/25.00,8017/N/25.00,8089/N/50.00'), '/N/',1,3) - 6),1,4) AS c,
(SUBSTR('8030/N/25.00,8017/N/25.00,8089/N/50.00',1,4) +
SUBSTR(substr('8030/N/25.00,8017/N/25.00,8089/N/50.00', - instr(reverse('8030/N/25.00,8017/N/25.00,8089/N/50.00'), '/N/',1,2) - 6),1,4) +
SUBSTR(substr('8030/N/25.00,8017/N/25.00,8089/N/50.00', - instr(reverse('8030/N/25.00,8017/N/25.00,8089/N/50.00'), '/N/',1,3) - 6),1,4)) AS total
FROM dual

使用字段名称:

SELECT SUBSTR(YOURFIELD,1,4) AS a,
SUBSTR(substr(YOURFIELD, - instr(reverse(YOURFIELD), '/N/',1,2) - 6),1,4) AS b,
SUBSTR(substr(YOURFIELD, - instr(reverse(YOURFIELD), '/N/',1,3) - 6),1,4) AS c,
(SUBSTR(YOURFIELD,1,4) +
SUBSTR(substr(YOURFIELD, - instr(reverse(YOURFIELD), '/N/',1,2) - 6),1,4) +
SUBSTR(substr(YOURFIELD, - instr(reverse(YOURFIELD), '/N/',1,3) - 6),1,4)) AS total
FROM YOURTABLE

只是总数:

SELECT (SUBSTR(YOURFIELD,1,4) +
SUBSTR(substr(YOURFIELD, - instr(reverse(YOURFIELD), '/N/',1,2) - 6),1,4) +
SUBSTR(substr(YOURFIELD, - instr(reverse(YOURFIELD), '/N/',1,3) - 6),1,4)) AS total
FROM YOURTABLE

答案 1 :(得分:0)

您的起始字符串似乎包含逗号分隔的值列表,其中每个值由3个由'/'分隔的信息组成。

如果是这样,通过首先识别所有项目,然后“解析”它们以提取所需信息并对结果求和,就可以了。

假设你有一张这样的表:

create table test(id, str) as (
    select 1, '8030/N/25.00,8017/N/25.00,8089/N/50.00' from dual union all
    select 2, '8029/N/25.00,8000/N/25.00,1000/N/50.00' from dual
)

您可以使用常用的分割字符串技术提取每行的项目:

SELECT id, regexp_substr(str, '[^,]+', 1, level) as item
  FROM test t
CONNECT BY instr(str, ',', 1, level - 1) > 0
  and prior id = id
  and prior sys_guid() is not null

给出:

        ID ITEM
---------- ----------------
         1 8030/N/25.00
         1 8017/N/25.00
         1 8089/N/50.00
         2 8029/N/25.00
         2 8000/N/25.00
         2 1000/N/50.00

现在你必须提取所需的信息(我假设它总是完全在前4个字符中)并且总和:

SELECT id, sum(to_char(substr(regexp_substr(str, '[^,]+', 1, level), 1, 4))) as the_sum
  FROM test t
CONNECT BY instr(str, ',', 1, level - 1) > 0
  and prior id = id
  and prior sys_guid() is not null   
group by id 

        ID    THE_SUM
---------- ----------
         1      24136
         2      17029

如果要求的信息不完全在前4个字符中,则只需编辑SUBSTR一些REGEXP_SUBSTR或任何您需要的内容,具体取决于字符串的结构。

答案 2 :(得分:0)

Oracle安装程序

CREATE TABLE table_name ( value ) As
  SELECT '8030/N/25.00,8017/N/25.00,8089/N/50.00' FROM DUAL;

<强>查询

SELECT TO_NUMBER( REGEXP_SUBSTR( value, '(^|,)(\d+)', 1, 1, NULL, 2 ) )
       + TO_NUMBER( REGEXP_SUBSTR( value, '(^|,)(\d+)', 1, 2, NULL, 2 ) )
       + TO_NUMBER( REGEXP_SUBSTR( value, '(^|,)(\d+)', 1, 3, NULL, 2 ) )
         AS total
FROM   table_name;

<强>输出

TOTAL
-----
24136