0

Say I want to check if a title name contains "stack" or contains "over" or contains "flow". Can I shorten the below code?

select name 
from Title
where name like '%stack%' or name like '%over%' or name like '%flow%'
;

Thanks!

2
  • Can't find an obvious way to shorten the query using LIKE. You could use REGEXP_LIKE, however that may have performance implications so may not necessarily be a good idea just to type less SQL. Let me know if you want me to add an example as an answer. Commented Mar 14, 2016 at 5:03
  • Yeah I can try that, if it's not a huge hindrance on speed then it should be okay. Commented Mar 14, 2016 at 5:13

1 Answer 1

1

You could use REGEXP_LIKE, however that may have performance implications, so you may want to test it for your specific use case;

SELECT name 
FROM Title
WHERE REGEXP_LIKE(name, '.*(stack|over|flow).*')

...or if you want the match case insensitive you can pass i as a third parameter;

SELECT name 
FROM Title
WHERE REGEXP_LIKE(name, '.*(stack|over|flow).*', 'i');

A regex101 explaining the regex itself.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.