PostgreSQL – Substring Function
Last Updated :
13 Nov, 2024
PostgreSQL is a powerful relational database management system with extensive text processing functions, including the flexible SUBSTRING
function. This function enables users to extract specific portions of a string, making it essential for text manipulation, especially when dealing with large datasets or complex data structures.
In this article, we’ll explore the PostgreSQL SUBSTRING
function in detail, including its syntax, key terms, and practical examples for real-world applications.
What is the PostgreSQL Substring Function?
The SUBSTRING
function in PostgreSQL is designed to extract a defined part of a string. This function is highly useful for retrieving parts of text based on start and length parameters, or even based on regular expression patterns. The SUBSTRING
function can be combined with other string functions, making it a flexible tool for data extraction and manipulation in PostgreSQL queries.
Syntax
SUBSTRING ( string, start_position, length )
Key Terms
'string'
: This is the source string from which the substring will be extracted. The data type can be char
, varchar
, text
, etc.
'start_position'
: This integer value specifies the starting position from where the substring will be extracted.
'length'
: This integer value determines the number of characters to be extracted from the starting position.
Examples of PostgreSQL Substring Function
Let us take a look at some of the examples of the Substring Function in PostgreSQL to better understand the concept. In the following examples, we will demonstrate how to use the SUBSTRING
function for various use cases, showcasing its flexibility and practical applications in PostgreSQL.
Example 1: Extracting Initials from Names
Here we query data from the customer table. Here we operate on the ‘last_name’ and ‘first_name’ column. We will extract the initial name by querying the first character of the ‘first_name’ column from the customer table of the sample database.
Query:
SELECT
last_name,
SUBSTRING( first_name, 1, 1 ) AS initial
FROM
customer
ORDER BY
last_name;
Output

Explanation:
This query selects the last name and the first character of the first name, renaming it as “initial.” Results are ordered by last_name
.
Example 2: Extracting a Substring Using a Pattern
This example uses a regular expression to extract a specific pattern from a text string. Here, we’ll extract up to four consecutive digits to capture a house number. The following statement extracts the house number (maximum 4 digits, from 0 to 9) from a string:
Query:
SELECT
SUBSTRING ( 'The house no. is 9001', '([0-9]{1, 4})' ) AS house_no;
Output

Explanation:
This query uses a regular expression to extract up to four consecutive digits from the input string. This query applies a regular expression pattern to extract up to four digits from the input string, returning the house number “9001.”
Important Points About PostgreSQL Substring Function
- Pattern Matching Support: The
SUBSTRING
function in PostgreSQL supports regular expressions, allowing for advanced pattern matching.
- Combining Functions: The
SUBSTRING
function can be combined with JSON functions to extract substrings from JSON data.
- Multi-byte Character Encodings: PostgreSQL’s
SUBSTRING
function supports multi-byte character encodings, such as UTF-8.
- Case-insensitive Extraction: Combine
SUBSTRING
with LOWER
or UPPER
functions to perform case-insensitive substring extraction.
Conclusion
The PostgreSQL SUBSTRING
function is a highly flexible tool that enhances text processing capabilities by allowing targeted string extraction. Whether working with fixed positions or regular expression patterns, the SUBSTRING
function helps PostgreSQL users manage and manipulate textual data with ease.
By mastering the SUBSTRING
function and related text processing functions in PostgreSQL, developers can create efficient queries for handling textual data, making this function a must-know for effective data manipulation.
Similar Reads
PostgreSQL String Functions
PostgreSQL is a powerful, open-source relational database management system that offers a rich set of functions and operators for working with string data. String manipulation is an essential task in many applications, and PostgreSQL provides a variety of built-in functions to make working with text
8 min read
PostgreSQL - STRING_AGG() Function
The STRING_AGG() function in PostgreSQL is a powerful aggregate function used to concatenate a list of strings with a specified separator. This function is essential for combining string values from multiple rows into a single string, making data aggregation more efficient and readable. Let us get a
3 min read
PostgreSQL - TRIM Function
The TRIM() function in PostgreSQL is an essential tool for removing unwanted characters from strings. Whether we're working with user inputs, formatting text, or performing data cleansing operations, TRIM() is an invaluable function for managing string data. This article will provide an in-depth loo
4 min read
PostgreSQL - RIGHT Function
The PostgreSQL RIGHT() function, allows you to extract a specified number of characters from the right side of a string. This function can be incredibly useful for various text-processing tasks. Let us get a better understanding of the RIGHT Function in PostgreSQL from this article. SyntaxRIGHT(stri
2 min read
PostgreSQL - UPPER Function
In PostgreSQL, the UPPER function is utilized to convert a string into uppercase. This function is handy when you need to standardize text data by converting it to a uniform case, especially for comparison or display purposes. Let us get a better understanding of the UPPER Function in PostgreSQL fro
2 min read
SQL | String functions
SQL String Functions are powerful tools that allow us to manipulate, format, and extract specific parts of text data in our database. These functions are essential for tasks like cleaning up data, comparing strings, and combining text fields. Whether we're working with names, addresses, or any form
8 min read
PostgreSQL - Function Returning A Table
In PostgreSQL, the ability to create functions that return tables enhances the power and flexibility of our database operations. These PostgreSQL RETURN TABLE functions allow us to execute complex queries and return structured data efficiently. In this article, we will explain the PostgreSQL functio
5 min read
PostgreSQL - REPLACE() Function
PostgreSQL REPLACE() function is a powerful tool for efficient string manipulation within our database. By utilizing the REPLACE() syntax in PostgreSQL, we can easily replace specific substrings within a string and enabling us to clean, format and modify data effectively. In this article, We will le
4 min read
PostgreSQL REVERSE() Function
The REVERSE() function in PostgreSQL is a simple yet powerful tool used to reverse the order of characters in a given string. It takes one input which is a string and returns the characters in reverse order. This function is helpful when you need to transform data, run tests or validate information.
4 min read
PostgreSQL - SPLIT_PART Function
The PostgreSQL SPLIT_PART() function is a powerful tool for splitting strings based on a specific delimiter, returning a specified part of the string. This function is particularly useful when working with structured data in text format, such as CSV values or delimited dates, and enables efficient d
4 min read