Sling Academy
Home/PostgreSQL/Using the SUBSTRING function in PostgreSQL

Using the SUBSTRING function in PostgreSQL

Last updated: January 06, 2024

Overview

The SUBSTRING function is a powerful feature available for string manipulation in PostgreSQL databases. Its usefulness is recognized across various domains where data manipulation is an integral part of managing databases. Introduction

The SUBSTRING function has been a part of the SQL standard for many years and has been implemented in PostgreSQL along with its inception. Well-documented since the early versions, its functionality has been essential for any text processing within the database. Purpose of SUBSTRING

Uses

This function is used to extract a substring from a string based on a specified position and length. It is a key tool for string data manipulation, allowing users to easily retrieve portions of text for analysis, reports, and data transformation. Syntax and Parameters

Syntax & Parameters

The basic syntax of the SUBSTRING function in PostgreSQL is as follows:

SUBSTRING(string FROM start FOR length)

Where:

  • string: The source string from which you want to extract the substring.
  • start: The start position for the substring extraction. Positions start at 1.
  • length: The number of characters to extract.

The function returns the specified portion of the string passed in the ‘string’ argument.

Practical Examples

Example 1: Basic Substring Extraction

This example shows the basic usage of the SUBSTRING function to get a portion of the text.

SELECT SUBSTRING('PostgreSQL' FROM 1 FOR 5);
-- Output: 'PostG'

Example 2: Substring Extraction with SQL Variables

The SUBSTRING function can also be combined with SQL variables.

DO $
DECLARE
    my_str VARCHAR := 'Data Extraction';
    start_pos INT := 6;
    len INT := 10;
BEGIN
    RAISE NOTICE '%', SUBSTRING(my_str FROM start_pos FOR len);
END$;
-- Output: 'Extraction'

Example 3: Substring from Pattern Matching

PostgreSQL allows extracting substrings by pattern matching with regular expressions.

SELECT SUBSTRING('Foo1234 Bar5678', 'Bar(\d+)');
-- Output: 'Bar5678'

Conclusion

The SUBSTRING function in PostgreSQL is an indispensable tool for developers and database administrators alike. It allows for flexible string manipulation, contributing to data parsing, formatting, and analysis tasks. By mastering this function, one can significantly manipulate and understand their database text data with greater precision.

Next Article: Using the REPEAT function in PostgreSQL

Previous Article: Using the STRPOS Function in PostgreSQL

Series: PostgreSQL Tutorials: From Basic to Advanced

PostgreSQL

You May Also Like

  • PostgreSQL with TimescaleDB: Querying Time-Series Data with SQL
  • PostgreSQL Full-Text Search with Boolean Operators
  • Filtering Stop Words in PostgreSQL Full-Text Search
  • PostgreSQL command-line cheat sheet
  • How to Perform Efficient Rolling Aggregations with TimescaleDB
  • PostgreSQL with TimescaleDB: Migrating from Traditional Relational Models
  • Best Practices for Maintaining PostgreSQL and TimescaleDB Databases
  • PostgreSQL with TimescaleDB: Building a High-Performance Analytics Engine
  • Integrating PostgreSQL and TimescaleDB with Machine Learning Models
  • PostgreSQL with TimescaleDB: Implementing Temporal Data Analysis
  • Combining PostgreSQL, TimescaleDB, and Airflow for Data Workflows
  • PostgreSQL with TimescaleDB: Visualizing Real-Time Data with Superset
  • Using PostgreSQL with TimescaleDB for Energy Consumption Analysis
  • PostgreSQL with TimescaleDB: How to Query Massive Datasets Efficiently
  • Best Practices for Writing Time-Series Queries in PostgreSQL with TimescaleDB
  • PostgreSQL with TimescaleDB: Implementing Batch Data Processing
  • Using PostgreSQL with TimescaleDB for Network Traffic Analysis
  • PostgreSQL with TimescaleDB: Troubleshooting Common Performance Issues
  • Building an IoT Data Pipeline with PostgreSQL and TimescaleDB