15

I need to lowercase array content in PostgreSQL, lower(array['array_content'])) or array[lower('array_content'])) does not work. Actual array is much, much longer.

SELECT * FROM kliendi_aadress WHERE lower(linn) LIKE ANY (array['Tallinn', 'Tartu','Narva'])

Can this even be done?

3 Answers 3

13

the first that come to mind is ugly:

db=# select lower(array['Tallinn', 'Tartu','Narva']::text)::text[];
         lower
-----------------------
 {tallinn,tartu,narva}
(1 row)

Here I lower text representation of your array and then cast it back to array.

And so comparison:

db=# select 'tartu' = any (lower(array['Tallinn', 'Tartu','Narva']::text)::text[]);
 ?column?
----------
 t
(1 row)
Sign up to request clarification or add additional context in comments.

1 Comment

I wonder the double casts will cause any potential performance issues? Assume if we already handled all the inputs correctly
13

Well ILIKE solved this problem for me

SELECT * FROM kliendi_aadress WHERE linn ILIKE ANY (array['Tallinn', 'Tartu','Narva']);

Comments

3

The citext extension can come in handy in this situation.

-- If you haven't enabled citext in your DB
CREATE EXTENSION citext;
select 'tartu' = any (array['Tallinn', 'Tartu','Narva']::citext[])

should work.

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.