How to SELECT if String Contains a Substring Match in PostgreSQL

Bilal Shahid Feb 12, 2024
  1. How to SELECT if String Contains a Substring Match in PostgreSQL With the LIKE Operator
  2. How to SELECT if String Contains a Substring Match in PostgreSQL With the ILIKE Operator
  3. How to SELECT if String Contains a Substring Match in PostgreSQL With the POSITION Function
  4. How to SELECT if String Contains a Substring Match in PostgreSQL With the SIMILAR TO Operator
  5. How to SELECT if String Contains a Substring Match in PostgreSQL Using POSITION With the SUBSTRING Function
  6. How to SELECT if PostgreSQL String Contains a Substring Match With the REGEXP_MATCHES Function
  7. Conclusion
How to SELECT if String Contains a Substring Match in PostgreSQL

In PostgreSQL, robust substring matching is a common requirement for querying and extracting relevant information from strings. This article explores various methods to SELECT rows based on substring matches, providing a versatile toolkit for developers and database administrators.

From the basic LIKE and ILIKE operators to more advanced tools like REGEXP_MATCHES and POSITION with SUBSTRING, each method offers distinct capabilities to handle diverse substring matching scenarios.

How to SELECT if String Contains a Substring Match in PostgreSQL With the LIKE Operator

The LIKE operator is a powerful tool for pattern matching, enabling us to filter results based on the presence of a particular substring within a PostgreSQL string.

The basic syntax of the LIKE operator involves using wildcard characters to represent unknown parts of a string. The % percent sign is used as a wildcard character to match any sequence of characters, and the underscore sign _ represents a single character.

Here’s the syntax of the LIKE operator:

SELECT * FROM table_name WHERE column_name LIKE pattern;

In the context of substring matching, we use the % percent sign to signify any sequence of characters before and/or after the substring we are looking for. The LIKE operator returns True if the string contains the specific sub-string; else, False.

Let’s consider a practical example where we have a table named example_table with a column named example_column. We want to select rows where the column contains the substring example using the LIKE expression.

Take a look at the following query:

Example 1: Match at the Beginning With the LIKE Operator

-- Create a table
CREATE TABLE example_table (
    example_column TEXT
);

-- Insert some values
INSERT INTO example_table VALUES ('example123'), ('somethingelse'), ('example456'), ('789example');

-- Select rows where the column starts with the string 'example' using the LIKE operator
SELECT * FROM example_table WHERE example_column LIKE 'example%';

In this example, we are using the % wildcard character at the end of the pattern to match any characters that come after example. The result will include rows where the column starts with example.

Code Output:

Use LIKE Operator to Find String Match

Example 2: Match at the End With the LIKE Operator

-- Select rows where the column ends with 'example' using the LIKE operator
SELECT * FROM example_table WHERE example_column LIKE '%example';

Conversely, by placing the % wildcard at the beginning of the pattern, we match any characters that precede example. This query retrieves rows where the column ends with example.

Code Output:

Use LIKE Operator to Find String Match

Example 3: Match Anywhere in the String With the LIKE Operator

-- Select rows where the column contains 'example' anywhere using the LIKE operator
SELECT * FROM example_table WHERE example_column LIKE '%example%';

When % is used on both sides of the substring, we match any sequence of characters before and after example. This query returns rows where the substring is present anywhere in the column.

Code Output:

Use LIKE Operator to Find String Match

Example 4: Match Substring With Rogue Characters Using the LIKE Operator

Suppose we have a table named rogue_characters_table with a column named rogue_column, and we want to select rows where the column contains the substring abc%def, considering % as a literal character.

-- Create a table
CREATE TABLE rogue_characters_table (
    rogue_column TEXT
);

-- Insert values with rogue characters
INSERT INTO rogue_characters_table VALUES ('abc%def123'), ('ghijklabc%defmn'), ('xyz');

-- Select rows where the column contains 'abc%def' (escaping rogue characters) using the LIKE operator
SELECT * FROM rogue_characters_table WHERE rogue_column LIKE 'abc\%def%' ESCAPE '\';

In this example, we insert values into the rogue_characters_table and then use the LIKE operator with the ESCAPE clause to match rows where the rogue_column contains the substring abc%def.

The ESCAPE clause allows us to specify a character (in this case, \) to escape the rogue characters, treating % as a literal character.

Code Output:

 rogue_column
--------------
 abc%def123

How to SELECT if String Contains a Substring Match in PostgreSQL With the ILIKE Operator

In some cases, you might want to perform a case-insensitive search for substring matches.

The ILIKE operator emerges as a versatile ally when the need for case-insensitive substring matching arises. This operator expands on the capabilities of its sibling, the LIKE operator, by enabling us to perform substring matches without being sensitive to the case of the characters involved.

The syntax of the ILIKE operator mirrors that of the LIKE operator with an additional I, indicating case-insensitivity. The % percentage sign remains the wildcard representing any sequence of characters, and the underscore sign _ still signifies a single character.

The basic structure is as follows:

SELECT * FROM table_name WHERE column_name ILIKE pattern;

The key advantage of the ILIKE operator lies in its ability to disregard case distinctions, allowing us to get the matched substring regardless of whether they are in uppercase, lowercase, or a mix of both.

Let’s continue with our previous example using a table named example_table and a column named example_column. We aim to select rows where the column contains the case-insensitive substring example.

Example: Case-Insensitive Matching at the Beginning

-- Create a table
CREATE TABLE example_table (
    example_column TEXT
);

-- Insert some values
INSERT INTO example_table VALUES ('example123'), ('SomethingElse'), ('Example456'), ('789Example');

-- Select rows where the column starts with the string 'example' (case-insensitive) using the ILIKE operator
SELECT * FROM example_table WHERE example_column ILIKE 'example%';

In the first example above, we create a table example_table and insert values into the example_column. The above statement employs the ILIKE operator with the pattern example% to retrieve rows where the column starts with an example, irrespective of case differences.

Code Output:

Use LIKE Operator to Find String Match

The LIKE and ILIKE operators in PostgreSQL serve as valuable tools for selecting rows based on substring matches, offering a balance between simplicity and effectiveness. The LIKE operator is suitable for exact substring matches, while the ILIKE operator extends this functionality to be case-insensitive.

How to SELECT if String Contains a Substring Match in PostgreSQL With the POSITION Function

In PostgreSQL, the POSITION function stands as another reliable tool for discovering whether a substring exists within a given string. This function returns the index of the first occurrence of a specified substring within the main string.

The basic syntax of the POSITION function involves specifying the substring to be located within the main string.

SELECT * FROM table_name WHERE POSITION(substring IN main_string) > 0;

The condition POSITION(substring IN main_string) > 0 checks whether the substring is present in the main string, ensuring that the index returned is greater than zero.

It returns the index of the substring’s first match occurrence and zero if the substring is not found in the string. Otherwise, the function fails and returns null.

Let’s continue using our hypothetical table named substring_table with a column name substring_column to showcase the usage of the POSITION function.

Example 1: Select Rows With Substring example

-- Create a table
CREATE TABLE substring_table (
    substring_column TEXT
);

-- Insert values with various substrings
INSERT INTO substring_table VALUES ('example123'), ('somethingelse'), ('anotherexample');

-- Select rows where the column contains the sub string 'example'
SELECT * FROM substring_table WHERE POSITION('example' IN substring_column) > 0;

In the example above, we begin by creating a table substring_table and populating it with values in the substring_column. The subsequent SELECT statement utilizes the POSITION function to check for the presence of the substring example in each row of the substring_column.

The condition POSITION('example' IN substring_column) > 0 ensures that only rows with a positive index (indicating the presence of the substring) are selected. This approach is robust, as it considers both the existence and the position of the substring within the string.

Code Output:

Use POSITION Operator to Find String Match

Let’s delve into additional example to further illustrate the use of the POSITION function in PostgreSQL for substring matching.

Example 2: Match Substring With Escape Characters

-- Insert values with escape characters
INSERT INTO substring_table VALUES ('abc%def'), ('xyz%example%');

-- Select rows where the PostgreSQL string contains 'abc%def' using escape characters
SELECT * FROM substring_table WHERE POSITION('abc%def' IN substring_column) > 0;

Expanding on the escape character concept, this example involves inserting values with the substring abc%def and using the POSITION function to select rows where this substring is present. The escape characters ensure the % symbol is treated as a literal character in the matching process.

Code Output:

 substring_column
------------------
 abc%def

The POSITION function proves valuable when precise information about the location of the specified substring is essential. It’s particularly useful in scenarios where you need to know not only if a substring is present but also where it occurs in the main string.

How to SELECT if String Contains a Substring Match in PostgreSQL With the SIMILAR TO Operator

PostgreSQL also offers the SIMILAR TO operator, providing a more advanced and flexible approach to substring matching using regular expressions.

The SIMILAR TO operator allows the use of regular expressions for more sophisticated pattern matching. The syntax is akin to the LIKE operator, but with the added flexibility of regular expressions:

SELECT * FROM table_name WHERE column_name SIMILAR TO pattern;

The SIMILAR TO operator allows for nuanced substring matching using regular expressions, offering a broader range of possibilities compared to simple pattern matching operators.

Let’s continue using our table similar_to_table with a column name similar_to_column to showcase the usage of the SIMILAR TO operator. Take a look at the following query:

Example 1: Match Substring With Alternatives

-- Create a table
CREATE TABLE similar_to_table (
    similar_to_column TEXT
);

-- Insert values with various substrings
INSERT INTO similar_to_table VALUES ('abc123'), ('123def'), ('xyz');

-- Select rows where the column contains 'abc' or 'def' as substrings
SELECT * FROM similar_to_table WHERE similar_to_column SIMILAR TO '%(abc|def)%';

In this example, we create the similar_to_table, insert values into the similar_to_column, and use the SIMILAR TO operator to select rows where the column contains either abc or def as substrings. The regular expression (abc|def) denotes an alternative match.

Code Output:

Use SIMILAR TO Operator to Find String Match

Example 2: Disable Meta-Characters in Substring Matching

-- Insert values with meta-characters
INSERT INTO similar_to_table VALUES ('xy*z'), ('pqr+abc');

-- Select rows where the PostgreSQL string contains the literal string '*z'
SELECT * FROM similar_to_table WHERE similar_to_column SIMILAR TO '%\*z%';

This example involves inserting values with meta-characters, such as * and +, and using the SIMILAR TO operator to select rows where the column contains the literal substring *z. The backslash \ serves as an escape character, disabling the meta-character * in the matching process.

Code Output:

 similar_to_column
-------------------
 xy*z

The SIMILAR TO operator proves advantageous when more complex and nuanced substring matching is required, such as using alternatives and handling meta-characters.

How to SELECT if String Contains a Substring Match in PostgreSQL Using POSITION With the SUBSTRING Function

Combining the POSITION function with the SUBSTRING function can be a formidable approach to precisely select rows based on substring occurrences within a string. This dynamic duo allows for a granular examination of string contents, providing valuable insights into substring presence and location.

As discussed earlier, the POSITION function returns the index of the first occurrence of a specified substring within the main string. When used with SUBSTRING, it becomes a potent tool for substring matching.

The syntax involves checking if the result of the POSITION function is greater than zero:

SELECT * FROM table_name WHERE POSITION(substring IN SUBSTRING(column_name FROM start_position)) > 0;

Here, substring is the target substring, and start_position is the position in the main PostgreSQL string where the search begins.

Let’s continue using our table substring_position_table with a column named substring_position_column to demonstrate the usage of POSITION with the SUBSTRING function.

Example 1: Select Rows With Exact Substring Match

-- Create a table
CREATE TABLE substring_position_table (
    substring_position_column TEXT
);

-- Insert values with various substrings
INSERT INTO substring_position_table VALUES ('apple123'), ('orange456'), ('banana789');

-- Select rows where the column contains the exact substring 'orange'
SELECT * FROM substring_position_table WHERE POSITION('orange' IN SUBSTRING(substring_position_column FROM 1)) > 0;

In this example, we create the substring_position_table, insert values into the substring_position_column, and use the POSITION function with the SUBSTRING function to select rows where the column contains the exact substring orange.

The SUBSTRING(substring_position_column FROM 1) ensures that the search starts from the beginning of the PostgreSQL string.

Code Output:

Use SIMILAR TO Operator to Find String Match

Example 2: Select Rows With Substring Starting From a Specific Position

-- Select rows where PostgreSQL string contains the substring '789' starting from position 7
SELECT * FROM substring_position_table WHERE POSITION('789' IN SUBSTRING(substring_position_column FROM 7)) > 0;

This example takes it a step further by starting the search from position 7 in the substring_position_column.

The condition POSITION('789' IN SUBSTRING(substring_position_column FROM 7)) > 0 checks for the presence of the substring 789 from the specified starting position.

Code Output:

 substring_position_column
---------------------------
 banana789

The combination of POSITION with SUBSTRING offers flexibility in pinpointing substring matches within the PostgreSQL string.

Whether you need an exact match or want to start searching from a specific position, incorporating these techniques into your PostgreSQL queries empowers you to perform intricate substring matching with precision.

How to SELECT if PostgreSQL String Contains a Substring Match With the REGEXP_MATCHES Function

When it comes to intricate substring matching in PostgreSQL, the REGEXP_MATCHES function can also be used. This function allows for pattern matching using regular expressions, providing unparalleled flexibility.

The syntax involves specifying the target string, the regular expression pattern, and optional flags:

SELECT * FROM table_name WHERE REGEXP_MATCHES(column_name, 'pattern', 'flags');

Here, column_name is the target column name, pattern is the regular expression to match, and flags are optional parameters to modify the matching behavior.

Let’s continue using our table regexp_matches_table with a column named regexp_matches_column to showcase the usage of the REGEXP_MATCHES function. Take a look at the following query:

Example 1: Match Substring With Alternatives

-- Create a table
CREATE TABLE regexp_matches_table (
    regexp_matches_column TEXT
);

-- Insert values with various substrings
INSERT INTO regexp_matches_table VALUES ('apple123'), ('orange456'), ('banana789');

-- Select rows where the column contains 'apple' or 'orange' as substrings using alternatives
SELECT * FROM regexp_matches_table WHERE regexp_matches_column ~ '(apple|orange)';

In this example, we create the regexp_matches_table, insert values into the regexp_matches_column, and use the REGEXP_MATCHES function to select rows where the column contains either apple or orange as substrings. The regular expression (apple|orange) denotes an alternative match.

Code Output:

Use SIMILAR TO Operator to Find String Match

Example 2: Substring Pattern Matching With Meta-Characters

-- Insert values with meta-characters
INSERT INTO regexp_matches_table VALUES ('xy*z'), ('pqr+abc');

-- Select rows where the column contains the literal substring '*z' using escape characters
SELECT * FROM regexp_matches_table WHERE regexp_matches_column ~ E'\\*z';

Here, we delve into matching substrings with meta-characters. We insert values with meta-characters like * and +, and use the REGEXP_MATCHES function with the pattern E'\\*z' to selectively match rows where the column contains the literal substring *z.

The E before the PostgreSQL string denotes an escape string, and the double backslash \\ serves as an escape character, ensuring the meta-character * is treated as a literal character.

Code Output:

 regexp_matches_column
-----------------------
 xy*z

The REGEXP_MATCHES function opens up possibilities for intricate substring matching, making it a valuable tool in PostgreSQL queries.

Whether you need to use alternatives, handle meta-characters, or implement more advanced pattern matching, incorporating the REGEXP_MATCHES function empowers you to wield regular expressions for nuanced substring selection.

Conclusion

In PostgreSQL substring matching, the diverse methods explored in this article offer a spectrum of tools catering to different levels of complexity and precision.

From the foundational LIKE and ILIKE operators for straightforward matches to the nuanced power of regular expressions with REGEXP_MATCHES, users have a comprehensive set of options at their disposal.

The strategic use of POSITION with SUBSTRING brings an extra layer of control, while the SIMILAR TO operator expands possibilities with pattern matching. Choosing the right method depends on the specific demands of the task, showcasing the adaptability and richness of PostgreSQL in handling substring selection scenarios.

Armed with this knowledge, you can navigate through a variety of data scenarios, ensuring their queries are not only efficient but also tailored to the intricacies of their data.

Author: Bilal Shahid
Bilal Shahid avatar Bilal Shahid avatar

Hello, I am Bilal, a research enthusiast who tends to break and make code from scratch. I dwell deep into the latest issues faced by the developer community and provide answers and different solutions. Apart from that, I am just another normal developer with a laptop, a mug of coffee, some biscuits and a thick spectacle!

GitHub