Is there any way to write case-insensitive queries in PostgreSQL, E.g. I want that following 3 queries return same result.
SELECT id FROM groups where name='administrator'
SELECT id FROM groups where name='ADMINISTRATOR'
SELECT id FROM groups where name='Administrator'
The older PostgreSQL method for performing case-insensitive text operations is the citext type; it is similar to the text type, but operators are functions between citext values are implicitly case-insensitive. The PostgreSQL docs provide more information on this type.
Case insensitive SQL SELECT: Use upper or lower functions or this: select * from users where lower(first_name) = 'fred'; As you can see, the pattern is to make the field you're searching into uppercase or lowercase, and then make your search string also be uppercase or lowercase to match the SQL function you've used.
Introduction to SQL Case Insensitive SQL Case insensitivity is to use the query statements and the keywords tables and columns by specifying them in capital or small letters of alphabets. SQL keywords are by default set to case insensitive that means that the keywords are allowed to be used in lower or upper case.
No, these both are the same, just a different naming convention.
This may be too late for the original poster, but for completeness, the way to achieve case insensitive behaviour from PostgreSQL is to set a non-deterministic collation. This is only for Postgres 12. Details are described in docs here.
One of the ways to optimize queries in PostgreSQL where we are trying to find something in our database and have to provide it with a specific string to match along with the objects contained, we might need to use the same character cases that we used for that object while searching.
The citext extension allows you to define insensitive-case columns, so you can avoid things like the lower function. In fact, this extension calls the lower function automatically so that you don’t have to do it manually. If you’re using Ubuntu, you have to install the postgresql-contrib package.
But now we have another problem: the username column is still case-sensitive. This means that your WHERE clause will differentiate uppercase/lowercase characters. So, to return the records that matches john no matter the character case you’re using, you’ll have to use the lower function.
Use LOWER function to convert the strings to lower case before comparing.
Try this:
SELECT id
FROM groups
WHERE LOWER(name)=LOWER('Administrator')
using ILIKE
instead of LIKE
SELECT id FROM groups WHERE name ILIKE 'Administrator'
The most common approach is to either lowercase or uppercase the search string and the data. But there are two problems with that.
There are at least three less frequently used solutions that might be more effective.
CREATE INDEX ON groups (name::citext);
. (But see below.)CREATE
INDEX ON groups (LOWER(name));
. Having done that, you can take advantage
of the index with queries like SELECT id FROM groups WHERE LOWER(name) = LOWER('ADMINISTRATOR');
, or SELECT id FROM groups WHERE LOWER(name) = 'administrator';
You have to remember to use LOWER(), though.The citext module doesn't provide a true case-insensitive data type. Instead, it behaves as if each string were lowercased. That is, it behaves as if you had called lower()
on each string, as in number 3 above. The advantage is that programmers don't have to remember to lowercase strings. But you need to read the sections "String Comparison Behavior" and "Limitations" in the docs before you decide to use citext.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With