Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL: Case insensitive string comparison

Is there a simple ignore-case-comparison for PostgreSQL?

I want to replace:

SELECT id, user_name      FROM users          WHERE lower(email) IN (lower('[email protected]'), lower('[email protected]')); 

With something like:

SELECT id, user_name      FROM users          WHERE email IGNORE_CASE_IN ('[email protected]', '[email protected]'); 

The like and ilike operators work on single values (e.g. like '[email protected]'), but not on sets.

like image 529
Adam Matan Avatar asked Dec 19 '10 08:12

Adam Matan


People also ask

Is Postgres case-insensitive?

PostgreSQL names are case sensitive. By default, AWS Schema Conversion Tool (AWS SCT) uses object name in lowercase for PostgreSQL. In most cases, you'll want to use AWS Database Migration Service transformations to change schema, table, and column names to lower case.

How do I make a case-insensitive in PostgreSQL?

While using regular expressions, we need to use the PostgreSQL ~* operator instead of the like operator; we can also use the ilike operator in PostgreSQL. We can also create an extension name as citext to use the case insensitive query in PostgreSQL; we need to create it first to use the extension of citext.

How do you do case-insensitive comparison?

Comparing strings in a case insensitive manner means to compare them without taking care of the uppercase and lowercase letters. To perform this operation the most preferred method is to use either toUpperCase() or toLowerCase() function. Example 1: This example uses toUpperCase() function to compare two strings.

What does <> mean in PostgreSQL?

<> is the standard SQL operator meaning "not equal". Many databases, including postgresql, supports != as a synonym for <> . They're exactly the same in postgresql.


1 Answers

select *  where email ilike '[email protected]' 

ilike is similar to like but case insensitive. For escape character use replace()

where email ilike replace(replace(replace($1, '~', '~~'), '%', '~%'), '_', '~_') escape '~' 

or you could create a function to escape text; for array of text use

where email ilike any(array['[email protected]', '[email protected]']) 
like image 129
Bonshington Avatar answered Sep 17 '22 13:09

Bonshington