Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to filter with multiple conditions in like: Oracle SQL Developer

I want to optimise the following SQL request (without creating another table):

Select * from table
where var1 like 'file_X_2022_1_001%' 
   or var1 like 'file_X_2022_1_004%' 
   or var1 like 'file_X_2022_1_006%'
   or var1 like 'file_X_2022_8_002%'
   or var1 like 'file_X_2022_8_0015%' 
   .
   .
   .
   or ... or var1 like 'file_X_2022_10_1000%';

I want something like in() where I can regroup all values in one parentheses 🙂

Thank you in advance!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can reduce your filtering conditions in the WHERE clause to one only with REGEXP_LIKE.

SELECT * 
FROM tab
WHERE REGEXP_LIKE (var1, 'file_X_2022_[0-9]{1,2}_[0-9]?[0-9]{3}')

Pattern should be fine-tuned to exclude the values you don’t allow. Values pointed in the post by you, get all caught by this regex.

Check the demo here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading