regexp_substr with LIKE as search condition

709 views Asked by At

Thank you mathguy for your suggestion and assistance. The example you provided is a near perfect description of the issue. That being said I've used and edited your text to help describe this issue:

I receive a string that contains comma delimited digits in the form of 18656, 16380, 16424 (call this param1). The string only contains commas and digits. In mytable I have a column named t with values such as 18656.01.02, 10.02.02, 16380.02.03, 16424.05.66, 16424.55.23.14. I want to select the all rows that match all of the comma-separated digits in param1; where the first numeric component in column t is like 18656, 16380, 16424. Is there a way to use regexp_substr in this case.

Where param1 = 18656, 16380, 16424 the following works:

select * from mytable where t.mycolumn IN
    (
    (SELECT regexp_substr(:param1,'[^,]+', 1, level) as NUMLIST
    FROM DUAL
     CONNECT BY regexp_substr(:param1, '[^,]+', 1, level) IS NOT NULL)
    );

How to use wildcard if data I seek from t.mycolumn = 18656.00.01, 16380.09.34, 16424.023.8 Can LIKE be used as search criteria? If possible please provide example. Obviously, the following will not work but I am hoping to find a solution.

  select * from mytable where t.mycolumn LIKE
    (
    (SELECT regexp_substr(:param1||'%','[^,]+', 1, level) as NUMLIST
    FROM DUAL
     CONNECT BY regexp_substr(:param1||'%', '[^,]+', 1, level) IS NOT NULL)
    );
1

There are 1 answers

1
krokodilko On

Assumptions:

There is a table named mytable with a column named t which contains values as follows:

SELECT * FROM mytable;

T              |
---------------|
18656.01.02    |
10.02.02       |
16380.02.03    |
16424.05.66    |
16424.55.23.14 |

There is a string received as a parameter, that contains comma delimited digits in the form of 18656, 16380, 16424. The string only contains commas and digits. This string is parsed into indyvidual rows with a help of a query that looks similar to the folowing one:

SELECT regexp_substr(param1,'[^,]+', 1, level) as NUMLIST
FROM ( 
   select '18656,16380,16424' as param1 FROM DUAL
) 
CONNECT BY regexp_substr(param1, '[^,]+', 1, level) IS NOT NULL
;

NUMLIST |
--------|
18656   |
16380   |
16424   |

Requirement

Can LIKE be used as search criteria? If possible please provide example.

LIKE keyword is used below as a condition in JOIN ... ON clause:

SELECT * FROM mytable
WHERE t IN (
   SELECT t 
   FROM mytable m
   JOIN (
            SELECT regexp_substr(param1,'[^,]+', 1, level) as NUMLIST
            FROM ( 
               select '18656,16380,16424' as param1 FROM DUAL
            ) 
            CONNECT BY regexp_substr(param1, '[^,]+', 1, level) IS NOT NULL
   ) x
   ON m.t LIKE '%' || x.NUMLIST || '%'
 )

T              |
---------------|
18656.01.02    |
16380.02.03    |
16424.05.66    |
16424.55.23.14 |