1

I have table File with column file_names which is ARRAY type. W would like to build query searching records where any of array element is like "abc*" using wildcard.

I tried:

File.names.any("abc%", operator= operators.like_op).all()

but it doesn't work. It return sql:

SELECT file.id, file.names FROM file WHERE 'abc%' LIKE ANY(file.names)

but it return empty result. Do you have any suggestions, how to build such query?

0

2 Answers 2

1

The like operator applies to a text, not to an array, so you need to find a way to convert your array into text before applying the like operator. To do so in sql you can use array_to_string() function, see the manual.

For example :

select array_to_string('{"name_1", "name_2", "name_3"}' :: text[], ' ') LIKE '%name_2%' returns true.

The first character % is mandatory because the searched word may be at any place in the string which results from the array.

Then you can add the separator specified in the array_to_string() function before and after the searched word (LIKE '% name2 %') in order to exclude the results like somename_2.

For example :

select array_to_string('{"name_1", "name_2", "name_3"}' :: text[], ' ') LIKE '% name_2 %' returns true

but

select array_to_string('{"name_1", "somename_2", "name_3"}' :: text[], ' ') LIKE '% name_2 %' returns false.

Last but not least, for the seprator, you can specifiy a non printable character like chr(12) or a set of characters instead of the space character in order to secure the results of the query.

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

5 Comments

But I try to ask about elements of ARRAY which are strings and use LIKE to every element in ARRAY. If we say about array_to_string(), when I concatenate all elements the query with LIKE to this concatenated string doesn't give such efect as for every element separately.
@Wald, when using the array_to_string() function, you can specify the delimiter as a text, either with non printable ascii characters such as chr(12) or with a sequence of characters that cannot appear in the array elements. I would suggest the first solution.
f.e column names has value ['name_1', 'name_2', 'name_3']. After array_to_string() if I query for 'name_2%' I don't get this record because before 'name_2' is 'name_1', so it doesn't match pattern despite array element match pattern. It is a problem.
select array_to_string('{"name_1", "name_2", "name_3"}' :: text[], ' ') LIKE '%name_2%'returns true
You are right. But I don't want to ask about '%name_2%' but ask for name_2%, because I'm not interesting in results f.e somename_2
0

I found solution in SQL:

SELECT file.id, file.names FROM file WHERE EXISTS (SELECT 1 FROM unnest(file.names) AS n
WHERE n LIKE 'abc%')

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.