Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

I have a simple list of ~25 words. I have a varchar field in PostgreSQL, let's say that list is ['foo', 'bar', 'baz']. I want to find any row in my table that has any of those words. This will work, but I'd like something more elegant.

select *

from table

where (lower(value) like '%foo%' or lower(value) like '%bar%' or lower(value) like '%baz%')

1 Answer

0 votes
by (40.7k points)

Try using Postgres' SIMILAR TO operator which supports alternations.

Use this code: 

SELECT * 

FROM table 

WHERE lower(value) similar to '%(foo|bar|baz)%';

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...