简体   繁体   中英

Comma-separated string match

I have this query:

SELECT regexp_replace (var_called_num, '^' ||ROUTING_PREFIX) INTO Num  
FROM   INCOMING_ROUTING_PREFIX
WHERE  var_called_num LIKE ROUTING_PREFIX ||'%';`

INCOMING_ROUTING_PREFIX table has two rows

1) 007743
2) 007742

var_called_num is 0077438843212123 . So above query gives the result 8843212123 . So basically, the query is removing prefix (longest match from table) from var_called_num .

Now my table has changed. Now it has only 1 row which is comma-separated.

Modified Table: INCOMING_ROUTING_PREFIX table has one row which is comma-separated:

1) 007743,007742

How to modify the query to achieve the same behavior. Need to remove longest match prefix from var_called_num .

you can split the values

with test as (
select regexp_substr('007743,007742','[^,]+', 1, level) as ROUTING_PREFIX from dual
  connect by regexp_substr('007743,007742S', '[^,]+', 1, level) is not null
  )

and that use the view in your select

SELECT regexp_replace ('0077438843212123', '^' ||ROUTING_PREFIX)  
  FROM test WHERE '0077438843212123' LIKE ROUTING_PREFIX ||'%';  

Here's one option: you'd have to split the prefix into rows, and the use it in REGEXP_REPLACE .

SQL> with
  2    calnum (var_called_num) as
  3      (select '0077438843212123' from dual),
  4    incoming_routing_prefix (routing_prefix) as
  5      (select '007743,007742' from dual),
  6  --
  7    irp_split as
  8      (select regexp_substr(i.routing_prefix, '[^,]+', 1, level) routing_prefix
  9       from incoming_routing_prefix i
 10       connect by level <= regexp_count(i.routing_prefix, ',') + 1
 11      )
 12  select regexp_replace(c.var_called_num, '^' || s.routing_prefix) result
 13  from calnum c join irp_split s on s.routing_prefix = substr(c.var_called_num, 1, length(s.routing_prefix));

RESULT
----------------
8843212123

SQL>

By the way, why did you change the model to a worse version than it was before?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM