Using the STRPOS Function in PostgreSQL

Updated: January 5, 2024 By: Guest Contributor Post a comment

Introduction

The STRPOS function is a part of PostgreSQL’s string functions that allows you to locate the position of a substring within a string. It is equivalent to the CHARINDEX function in SQL Server and INSTR function in Oracle. Knowing how to effectively manipulate and search through strings is crucial for data management and queries in any SQL-compliant database, PostgreSQL included.

Background

PostgreSQL is an advanced, open-source object-relational database system with a strong reputation for reliability, feature robustness, and performance. While the STRPOS function itself has been available for many PostgreSQL versions, it continues to be a relevant tool for string manipulation in the latest iterations of the database system.

Purpose

The purpose of the STRPOS function is to find the first occurrence of a specified substring within another string. It is commonly used in data parsing, analysis, and reporting tasks where there is a need to locate the position of characters or substrings for validation, extraction, or manipulation.

Syntax, Parameters, and Return Value

The syntax of the STRPOS function in PostgreSQL is:

STRPOS(string, substring)

It takes two parameters:

  • string is the string from which you want to find the substring’s first occurrence.
  • substring is the substring whose position within the main string you are interested in finding.

The function returns an integer value representing the position of the first character of the first occurrence of the substring within the string. The indexing is 1-based, meaning if the substring does not exist in the string, the function will return 0.

Examples

Example 1: Finding a Substring’s Position

This example demonstrates how to find the position of a substring within a string.

We’re going to search for the word ‘PostgreSQL’ within a longer string to see at what character it begins.

-- Query:
 SELECT STRPOS('Learning SQL with PostgreSQL is fun', 'PostgreSQL') AS position;

-- Result:
-- position
-- 17

Example 2: Substring Not Found

An example of what happens when the substring is not found in the string.

We’ll search for the substring ‘MySQL’ in the string ‘Learning SQL with PostgreSQL is fun’ and observe the result.

-- Query:
 SELECT STRPOS('Learning SQL with PostgreSQL is fun', 'MySQL') AS position;

-- Result:
-- position
-- 0

Example 3: Case-Sensitive Search

Using STRPOS to perform a case-sensitive search.

In contrast to some other SQL functions or regular expressions, the STRPOS function is case-sensitive. This can impact the result as shown in this example.

-- Query:
 SELECT STRPOS('PostgreSQL', 'sql') AS position;

-- Result:
-- position
-- 0

Example 4: Using STRPOS in a WHERE Clause

Filtering rows in a table based on the position of a substring.

Here we are using the STRPOS function in a WHERE clause to find and retrieve rows where the substring ‘SQL’ appears in the column ‘course_title’.

-- Given a table 'courses' with a column 'course_title', we can query:
 SELECT *
 FROM courses
 WHERE STRPOS(course_title, 'SQL') > 0;

Conclusion

The STRPOS function in PostgreSQL is a simple yet powerful tool for working with strings. Its primary use is to locate the position of a substring, which can be handy in many day-to-day database operations including reporting, searching, and text analysis. Although the concept of this function is quite straightforward, mastery of string functions like STRPOS expands the capabilities of developers and database administrators in creating more complex and refined SQL queries.