从oracle中的字符串得出费用

时间:2018-06-27 12:25:31

标签: regex oracle11g

我的表格中有一个名为narration的列,其数据如下。

NARRATION

MMT/PS/12345/Family/RAM/chgRs5.00GSTRs0.90
MMT/43425/chgRs5.00GSTRs0.90/1257874554
MMT/XX/12345/Family/RAM/chgRs25.00GSTRs20.90
ITI/4425/chgRs15.00GSTRs10.70/1257874554

我要从叙述字段中导出费用和GST值,所需的输出如下所示

DERIVED NARRATION|Charges|GST

chgRs5.00GSTRs0.90|5.00|0.90
chgRs5.00GSTRs0.90|5.00|0.90
chgRs25.00GSTRs20.90|25.00|20.90
chgRs15.00GSTRs10.70|5.00|10.70

请建议查询。我尝试如下图所示

SELECT REGEXP_SUBSTR(REGEXP_SUBSTR('MMT/43425/chgRs5.00GSTRs0.90/1257874554','[^/]+',1,3),'[^GSTRs]+',1,2) ,
   REGEXP_SUBSTR(REGEXP_SUBSTR('MMT/43425/chgRs5.00GSTRs0.90/1257874554','[^/]+',1,2+1),'[^GSTRs]+',1,3)
  FROM  DUAL;

但是它不能为我提供所有情况的正确输出。

3 个答案:

答案 0 :(得分:1)

您不需要正则表达式:

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE table_name ( NARRATION ) AS
SELECT 'MMT/PS/12345/Family/RAM/chgRs5.00GSTRs0.90' FROM DUAL UNION ALL
SELECT 'MMT/43425/chgRs5.00GSTRs0.90/1257874554' FROM DUAL UNION ALL
SELECT 'MMT/XX/12345/Family/RAM/chgRs25.00GSTRs20.90' FROM DUAL UNION ALL
SELECT 'ITI/4425/chgRs15.00GSTRs10.70/1257874554' FROM DUAL;

查询1

SELECT CASE FIN
       WHEN 0
       THEN SUBSTR( narration, chgrs )
       ELSE SUBSTR( narration, chgrs, fin - chgrs )
       END AS derived_narration,
       SUBSTR( narration, chgrs + 5, gstrs - chgrs - 5 ) AS charges,
       CASE FIN
       WHEN 0
       THEN SUBSTR( narration, gstrs + 5 )
       ELSE SUBSTR( narration, gstrs + 5, fin - gstrs - 5 )
       END AS GST
FROM   (
  SELECT narration,
         INSTR( narration, 'chgRs' ) AS chgrs,
         INSTR( narration, 'GSTRs' ) AS gstrs,
         INSTR( narration, '/', INSTR( narration, 'GSTRs' ) ) AS fin
  FROM   table_name
)

Results

|    DERIVED_NARRATION | CHARGES |   GST |
|----------------------|---------|-------|
|   chgRs5.00GSTRs0.90 |    5.00 |  0.90 |
|   chgRs5.00GSTRs0.90 |    5.00 |  0.90 |
| chgRs25.00GSTRs20.90 |   25.00 | 20.90 |
| chgRs15.00GSTRs10.70 |   15.00 | 10.70 |

查询2 :但是,如果您确实想使用正则表达式,则可以使用:

/(chgRs(\d+\.\d+)GSTRs(\d+\.\d+))(/|$)

赞:

SELECT REGEXP_SUBSTR( narration, '/(chgRs(\d+\.\d+)GSTRs(\d+\.\d+))(/|$)', 1, 1, NULL, 1 )
         AS derived_naration,
       REGEXP_SUBSTR( narration, '/(chgRs(\d+\.\d+)GSTRs(\d+\.\d+))(/|$)', 1, 1, NULL, 2 )
         AS charges,
       REGEXP_SUBSTR( narration, '/(chgRs(\d+\.\d+)GSTRs(\d+\.\d+))(/|$)', 1, 1, NULL, 3 )
         AS gst
FROM   table_name

Results

|     DERIVED_NARATION | CHARGES |   GST |
|----------------------|---------|-------|
|   chgRs5.00GSTRs0.90 |    5.00 |  0.90 |
|   chgRs5.00GSTRs0.90 |    5.00 |  0.90 |
| chgRs25.00GSTRs20.90 |   25.00 | 20.90 |
| chgRs15.00GSTRs10.70 |   15.00 | 10.70 |

答案 1 :(得分:0)

这可能是获得所需内容的一种方式:

select regexp_substr(narration, 'chgRs[^\/]+') as derived_narration,
       regexp_substr(narration, '(chgRs)(.+)(GSTR)', 1, 1, '', 2) as charges,
       regexp_substr(narration, '(GSTRs)(\d+\.\d+)', 1, 1, '', 2) as GST
from yourTable

带有示例数据:

SQL> with yourTable(NARRATION) as (
  2      select 'MMT/PS/12345/Family/RAM/chgRs5.00GSTRs0.90' from dual union all
  3      select 'MMT/43425/chgRs5.00GSTRs0.90/1257874554' from dual union all
  4      select 'MMT/XX/12345/Family/RAM/chgRs25.00GSTRs20.90' from dual union all
  5      select 'ITI/4425/chgRs15.00GSTRs10.70/1257874554' from dual
  6  )
  7  select regexp_substr(narration, 'chgRs[^\/]+') as derived_narration,
  8         regexp_substr(narration, '(chgRs)(.+)(GSTR)', 1, 1, '', 2) as charges,
  9         regexp_substr(narration, '(GSTRs)(\d+\.\d+)', 1, 1, '', 2) as GST
 10  from yourTable;

DERIVED_NARRATION         CHARGES    GST
------------------------- ---------- ----------
chgRs5.00GSTRs0.90        5.00       0.90
chgRs5.00GSTRs0.90        5.00       0.90
chgRs25.00GSTRs20.90      25.00      20.90
chgRs15.00GSTRs10.70      15.00      10.70

答案 2 :(得分:0)

with data as (
    select 'MMT/PS/12345/Family/RAM/chgRs5.00GSTRs0.90' as narration from dual union
    select 'MMT/43425/chgRs5.00GSTRs0.90/1257874554' from dual union
    select 'MMT/XX/12345/Family/RAM/chgRs25.00GSTRs20.90' from dual union
    select 'ITI/4425/chgRs15.00GSTRs10.70/1257874554' from dual
)
select regexp_substr(narration, 'chgRs\d+(\.\d{1,2})?'),
       regexp_substr(narration, 'GSTRs\d+(\.\d{1,2})?')
from data