PostgreSQL SUBSTRING() Function
Summary: in this tutorial, you will learn how to use the PostgreSQL SUBSTRING()
function to extract a substring from a string.
Introduction to PostgreSQL SUBSTRING() function
The SUBSTRING()
function allows you to extract a substring from a string and return the substring.
Here’s the basic syntax of the SUBSTRING()
function:
In this syntax:
string
: This is an input string with the data type char, varchar, text, and so on.start_position
: This is an integer that specifies where in the string you want to extract the substring. Ifstart_position
equals zero, the substring starts at the first character of the string. Thestart_position
can be only positive. Note that in other database systems such as MySQL the SUBSTRING() function can accept a negativestart_position
.length
: This is a positive integer that determines the number of characters that you want to extract from the string beginning atstart_position
. If the sum ofstart_position
andlength
is greater than the number of characters in thestring
, the substring function returns the whole string beginning atstart_position
. Thelength
parameter is optional. If you omit it, theSUBSTRING
function returns the whole string started atstart_position
.
PostgreSQL offers another syntax for the SUBSTRING()
function as follows:
PostgreSQL provides another function named SUBSTR()
that has the same functionality as the SUBSTRING()
function.
PostgreSQL SUBSTRING() function examples
Let’s explore some examples of using the SUBSTRING()
function.
1) Basic SUBSTRING() function examples
The following example uses the SUBSTRING()
function to extract the first 8 characters from the string PostgreSQL:
Output:
In the example, we extract a substring that has a length of 8, starting at the first character of the PostgreSQL
string. The result is et PostgreS
as illustrated in the following picture:
The following example uses the SUBSTRING()
function to extract the first 8 characters from the PostgreSQL string:
Output:
In this example, we extract a substring started at position 8 and omit the length
parameter. The resulting substring starts at the position 8 to the rest of the string.
The following examples use the alternative syntax of the SUBSTRING()
function:
Output:
2) Using the PostgreSQL SUBSTRING() function with table data
We’ll use the customer
table from the sample database:
The following example uses the SUBSTRING()
function to retrieve the initial names of customers by extracting the first character of value in the first_name
column:
Output:
Extracting substring matching POSIX regular expression
In addition to the SQL-standard substring function, PostgreSQL allows you to extract a substring that matches a POSIX regular expression.
The following illustrates the syntax of the substring function with POSIX regular expression:
Or you can use the following syntax:
If the SUBSTRING() function finds no match, it returns NULL.
If the pattern
contains any parentheses, the SUBSTRING()
function returns the text that matches the first parenthesized subexpression.
The following example uses the SUBSTRING()
to extract the house number with 1 to 4 digits, from a string:
Output:
Extracting substring matching a SQL regular expression
Besides the POSIX regular expression pattern, you can use the SQL regular expression pattern to extract a substring from a string using the following syntax:
In this syntax:
string
: is a string that you want to extract the substring.escape-character
: the escape character.pattern
is a regular expression wrapped inside escape characters followed by a double quote ("). For example, if the character#
is the escape character, the pattern will be#"pattern#"
. In addition, thepattern
must match the entirestring
, otherwise, the function will fail and returnNULL
.
For example:
Output:
Summary
- Use the PostgreSQL
SUBSTRING()
functions to extract a substring from a string.