PostgreSQL

PostgreSQL ILIKE Operator

PostgreSQL is an open-source, general-purpose object-relational database management system. It implements and extends the SQL language with additional features like scalability, security, etc.

This guide will look at the ILIKE operator available in PostgreSQL.

Prerequisites

To perform the steps demonstrated in this guide, you will need the following components:

For ease of use, I’m also using phpPgAdmin, a web-based front-end for PostgreSQL (similar to phpMyAdmin). It’s directly available in the official Ubuntu package repos.

$ sudo apt install phppgadmin php-fpm

Note that phpPgAdmin will automatically install and configure the Apache2 server to serve the web front-end.

In addition, I also borrowed this sample DVD Rental database from PostgreSQL Tutorial.

PostgreSQL ILIKE Operator

Suppose you’re searching for information that matches a specific pattern. In PostgreSQL, you can utilize a couple of options: the LIKE operator and ILIKE operator.

For the most part, the ILIKE operator behaves similarly to the LIKE operator. However, the main distinguishing feature is the case sensitivity. The ILIKE operator performs case-insensitive pattern matching. In many situations, case-insensitive searches are preferable to case-sensitive searches.

The ILIKE operator supports two special wildcard characters:

  1. Percentage (%): matches for any sequence of characters.
  2. Underscore (_): matches for any single character.

The structure is as follows:

$ <string> ILIKE <pattern>;

Using the ILIKE Operator

As mentioned earlier, both the LIKE and ILIKE operators act the same except for case sensitivity. Learn more about using the LIKE operator in PostgreSQL.

Example 1

In the following example, we will perform a simple search on the contents of the table “customers” (first_name):

SELECT
    first_name,
        last_name
FROM
    customer
WHERE
    first_name ILIKE 'Al%';

Here, we defined the pattern to search for is “Al%”, meaning any number of characters can appear after “Al”.

Example 2

What if we wanted to search for first_name entries that end with some specific characters? In that case, the code would look like this:

SELECT
    first_name,
        last_name
FROM
    customer
WHERE
    first_name ILIKE '%e';

Here, the ILIKE operator matches all the entries that end with the character “e” at the end.

Example 3

In the next example, ILIKE matches any entry with the character “x” in any position:

SELECT
    first_name,
        last_name
FROM
    customer
WHERE
    first_name ILIKE '%x%';

Example 4

Let’s try combining both of the wildcard symbols:

SELECT
    first_name,
        last_name
FROM
    customer
WHERE
    first_name ILIKE '_oR%';

Here, the pattern “_oR%” describes that any single character is at the start, then must be followed by “oR”, and can have any number of characters at the end.

Example 5

What if we wanted the matching entries to be excluded from the result? Then, we would like to use NOT ILIKE.

In the following example, the result will exclude any result matching the pattern “%e”:

SELECT
    first_name,
        last_name
FROM
    customer
WHERE
    first_name NOT ILIKE '%e';

Conclusion

This article discusses the ILIKE operator in PostgreSQL with multiple examples. It provides the ability to perform pattern matching while ignoring cases. After following this guide, you’ll be prepared to implement ILIKE operators in your own PostgreSQL queries.

Interested in learning more about PostgreSQL? Check out the PostgreSQL sub-category. Learn more about copying tables from one database to another, using the FLOOR function, working with arrays, etc.

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.