0

I need to check whether a string contains(LIKE) one of the substrings from separated by space(" ") delimiter values.

For example, I need to select rows where c.name field (with 'TEST-DOWNEY' value for ex.) contains one of the substrings from 'TEST CLIN HOSP' string.

I try to use LIKE ANY operator, but it requires '%' char before and after each substring in the array:

SELECT c.name FROM contact c
WHERE c.name LIKE ANY (regexp_split_to_array('TEST CLIN HOSP', E' '))

But if I run it like this:

SELECT c.name FROM contact c
    WHERE c.name LIKE ANY (regexp_split_to_array('%TEST% %CLIN% %HOSP%', E' '))

it works.

So how can I dynamically add '%' chars to every substring in the array after splitting it to an array?

1 Answer 1

1
with contact (name) as (values ('TEST-DOWNEY'))
select c.name
from contact c
where (
    select sum(strpos(c.name, p))
    from regexp_split_to_table('TEST CLIN HOSP', E' ') p(p)
) > 0
;
    name     
-------------
 TEST-DOWNEY
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.