0 votes
1 view
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.4k points)

Try using Postgres' SIMILAR TO operator which supports alternations.

Use this code: 


FROM table 

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

Welcome to Intellipaat Community. Get your technical queries answered by top developers !