3

I'm trying to find a way to force an array to make it upper or lowercase. This is so that no matter what the user inputs they get a result. This is the query:

select * from table where any(:id) = databasecolumn

:id is an array of chars that the user inputs(can be lowercase or uppercase) and I need to make sure that whatever the user inputs they get a result.

This works as long as the user inputs in uppercase (because the database values are also uppercase). But when they input lowercase letters they get no response.

I tried this:

select * from table where any(upper(:id)) = upper(databasecolumn)

but this does not work because the function "upper" is not for arrays. It works fine when I do it with a single input but not arrays.

Do you have any pointers? I couldn't find an equivalent function for an array of varchars.

1
  • What are you trying to do? By default, isn't the comparison already case-insensitive? Why do you need to apply upper at all? Commented Mar 21, 2017 at 12:44

2 Answers 2

3

You could use ILIKE:

select * 
from table 
where databasecolumn ILIKE any(:id);

This:

with data (col) as (
  values ('one'), ('Two'), ('THREE')
)
select *
from data
where col ilike any(array['one', 'two', 'three']);

returns:

col  
-----
one  
Two  
THREE
Sign up to request clarification or add additional context in comments.

Comments

1

you can use double casting like here:

t=# with a as (select '{caSe1,cAse2}'::text[] r) select r,upper(r::text)::text[] from a where true;
       r       |     upper
---------------+---------------
 {caSe1,cAse2} | {CASE1,CASE2}
(1 row)

It neglects the benefits of using ANY though

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.