PostgreSQL Tutorial

  • Home
  • Administration
  • Views
  • Triggers
  • Stored Procedures
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
Home / PostgreSQL Functions / PostgreSQL Substring

PostgreSQL Substring

Summary: in this tutorial, we will introduce you to PostgreSQL substring function that extracts a substring from a string.

Introduction to PostgreSQL substring function

The substring function returns a part of string. The following illustrates the syntax of the substring function:

1
SUBSTRING ( string ,start_position , length )

Let’s examine each parameter in detail:

  • string is a string whose data type is char, varchar, text, etc.
  • start_position is an integer that specifies where you want to extract the substring. If start_position equals zero, the substring starts at the first character of the string. The start_position can be only positive. Though in other database systems such as MySQL the substring function can accept a negative start_position.
  • length  is a positive integer that determines the number of characters that you want to extract from the string beginning at start_position. If the sum of start_position and length is greater than the number of characters in the string, the substring function returns the whole string beginning at start_position. The length parameter is optional. If you omit the length parameter, the substring function returns the whole string started at start_position.

PostgreSQL substring examples

See the following examples:

1
2
3
4
SELECT
SUBSTRING ('PostgreSQL', 1, 8); -- PostgreS
SELECT
SUBSTRING ('PostgreSQL', 8); -- SQL

In the first statement, we extract a substring that has length of 8 and it is started at the first character of the PostgreSQL string. we get PostgreS as the result. See the following picture:

PostgreSQL substring function example

In the second statement, we extract a substring started at position 8 and we omit the length parameter. The substring is a string beginning at 8, which is SQL.

PostgreSQL substring function example with optional LENGH parameter

PostgreSQL provides another syntax of the substring function as follows:

1
substring(string from start_position for length);

In this form, PostgreSQL puts three parameters into one. See the following example:

1
2
3
4
SELECT
SUBSTRING ('PostgreSQL' FROM 1 FOR 8); -- PostgreS
SELECT
SUBSTRING ('PostgreSQL' FROM 8); -- SQL

The results are the same as the one in the first example.

In the following example, we query data from the customer table. We select last_name and first_name column. We get the initial name by extracting the first character of the first_name column.

1
2
3
4
5
6
7
SELECT
last_name,
SUBSTRING (first_name, 1, 1) AS initial
FROM
customer
ORDER BY
last_name;

PostgreSQL substring example

Extracting substring matching POSIX regular expression

In addition to the SQL-standard substring function, PostgreSQL allows you to use extract a substring that matches a POSIX regular expression. The following illustrates the syntax of the substring function with POSIX regular expression:

1
SUBSTRING(string FROM pattern)

Or you can use the following syntax:

1
SUBSTRING(string,pattern);

Note that if no match found, the substring function return a null value. If the pattern contains any parentheses, the substring function returns the text that matches the first parenthesized subexpression.

The following example extracts the house number (maximum 4 digits, from 0 to 9) from a string:

1
2
3
4
5
SELECT
SUBSTRING (
'The house no. is 9001',
'([0-9]{1,4})'
) as house_no

PostgreSQL substring POSIX example

Extracting substring matching SQL regular expression

Besides POSIX regular expression pattern, you can use SQL regular expression pattern to extract a substring from a string using following syntax:

1
SUBSTRING(string FROM pattern FOR escape-character)

This form of substring function accepts three parameters:

  • string is a string that you want to extract the substring.
  • escape-character: the escape character.
  • pattern is a SQL regular expression pattern. It must be wrapped inside escape characters followed by a double quote ("). For example, if # is the escape character, the pattern will be #"pattern#". In addition, the pattern must match the entire string, otherwise the substring function will fail and return a NULL value.

See the following examples:

1
2
3
4
5
6
7
8
9
10
11
SELECT SUBSTRING (
'PostgreSQL'
FROM
'%#"S_L#"%' FOR '#'
); -- SQL
 
SELECT SUBSTRING (
'foobar'
FROM
'#"S_Q#"%' FOR '#'
); -- NULLL

PostgreSQL provides another function named substr that has the same functionality as the substring function.

In this tutorial, we have shown you various forms of the PostgreSQL substring functions that allow you to extract substring based on start position and length, and also based on a regular expression.

Previous Tutorial: PostgreSQL REPLACE
Next Tutorial: PostgreSQL LENGTH Function

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Aggregate Functions

  • PostgreSQL COUNT Function
  • PostgreSQL AVG Function
  • PostgreSQL MAX Function
  • PostgreSQL MIN Function
  • PostgreSQL SUM Function

PostgreSQL Conditional Expressions

  • PostgreSQL CASE
  • PostgreSQL NULLIF
  • PostgreSQL COALESCE

PostgreSQL String functions

  • PostgreSQL CONCAT Function
  • PostgreSQL TRIM Function
  • PostgreSQL LENGTH Function
  • PostgreSQL REPLACE
  • PostgreSQL Substring
  • PostgreSQL Letter Case Functions

PostgreSQL Operators

  • PostgreSQL CAST

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL Recursive View
  • Learn PostgreSQL Recursive Query By Example
  • Creating Updatable Views Using the WITH CHECK OPTION Clause
  • PostgreSQL Upsert Using INSERT ON CONFLICT statement
  • How to Generate a Random Number in A Range
  • Using PostgreSQL ADD COLUMN to Add One or More Columns To a Table
  • PostgreSQL Character Types: CHAR, VARCHAR, and TEXT
  • Using PostgreSQL SERIAL To Create Auto-increment Column
  • PostgreSQL Boolean Data Type with Practical Examples
  • Understanding PostgreSQL Timestamp Data Types

More Tutorials

  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Functions
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2016 by PostgreSQL Tutorial Website. All Rights Reserved.