Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make "case-insensitive" query in Postgresql?

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'
like image 843
Jame Avatar asked Aug 10 '11 02:08

Jame


People also ask

How do I make a case-insensitive in PostgreSQL?

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.

How do you make a case-insensitive in SQL query?

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.

How do you query case-insensitive?

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.

Is Postgres order by case-insensitive?

No, these both are the same, just a different naming convention.

Is there a way to make PostgreSQL case insensitive?

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.

How to optimize queries in PostgreSQL?

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.

What is the use of citext in PostgreSQL?

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.

Is the username column case sensitive in SQL?

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.


3 Answers

Use LOWER function to convert the strings to lower case before comparing.

Try this:

SELECT id 
  FROM groups
 WHERE LOWER(name)=LOWER('Administrator')
like image 111
Chandu Avatar answered Oct 18 '22 19:10

Chandu


using ILIKE instead of LIKE

SELECT id FROM groups WHERE name ILIKE 'Administrator'
like image 26
Mohammad Reza Norouzi Avatar answered Oct 18 '22 20:10

Mohammad Reza Norouzi


The most common approach is to either lowercase or uppercase the search string and the data. But there are two problems with that.

  1. It works in English, but not in all languages. (Maybe not even in most languages.) Not every lowercase letter has a corresponding uppercase letter; not every uppercase letter has a corresponding lowercase letter.
  2. Using functions like lower() and upper() will give you a sequential scan. It can't use indexes. On my test system, using lower() takes about 2000 times longer than a query that can use an index. (Test data has a little over 100k rows.)

There are at least three less frequently used solutions that might be more effective.

  1. Use the citext module, which mostly mimics the behavior of a case-insensitive data type. Having loaded that module, you can create a case-insensitive index by CREATE INDEX ON groups (name::citext);. (But see below.)
  2. Use a case-insensitive collation. This is set when you initialize a database. Using a case-insensitive collation means you can accept just about any format from client code, and you'll still return useful results. (It also means you can't do case-sensitive queries. Duh.)
  3. Create a functional index. Create a lowercase index by using 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.

like image 160
Mike Sherrill 'Cat Recall' Avatar answered Oct 18 '22 19:10

Mike Sherrill 'Cat Recall'