How to parse data using REGEXP_SUBSTR?

前端 未结 3 1910
情深已故
情深已故 2021-01-27 22:56

I have a data set like this (see below) and I try to extract digits which are in form {variable_number_of_digits}{hyphen}{only_one_digit}:

with mcte as (
select          


        
3条回答
  •  梦谈多话
    2021-01-27 23:44

    Combining the delimiter split query with REGEXP_LIKE and pivot-ing the result you get this query working for up to 6 numbers. You will need to update the cols subquery and teh pivot list to be able to process more numbers per record. (Unfortunately this can't be done general in a static SQL).

    with mcte as (
      select 1 id, 'ILLD/ELKJS/00000000/ELKJS/FHSH' as addr from dual
      union all 
      select 2 id, 'ILLD/EFECTE/0116988-7-002/ADFA/ADFG' as addr from dual
      union all
      select 3 id, 'IIODK/1573230-0/2216755-7/' as addr  from dual
      union all
      select 4 id, '1-1/1573230-0/2216755-700/676-7' as addr from dual
    ),
    cols as (select  rownum colnum from dual connect by level < 6 /* (max) number of columns */),
    mcte2 as (select id, cols.colnum, (regexp_substr(addr,'[^/]+', 1, cols.colnum)) addr 
                  from mcte, cols where regexp_substr(addr, '[^/]+', 1, cols.colnum) is not null),
    mcte3 as (              
    select ID, 
    ROW_NUMBER() over (partition by ID order by COLNUM) as col_no, ADDR from mcte2
    where REGEXP_like(addr, '^[0-9]+-[0-9]$')
    )
    select * from mcte3
    PIVOT (max(addr)   for (col_no) in 
         (1 as "NUM1",
          2 as "NUM2",
          3 as "NUM3",
          4 as "NUM4",
          5 as "NUM5",
          6 as "NUM6"))
    order by id;
    

    this gives a result

            ID NUM1       NUM2       NUM3       NUM4       NUM5       NUM6     
    ---------- ---------- ---------- ---------- ---------- ---------- ----------
             3 1573230-0  2216755-7                                              
             4 1-1        1573230-0  676-7       
    

提交回复
热议问题