PostgreSQL - SPLIT_PART Function
Last Updated :
08 Nov, 2024
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 data extraction and manipulation in SQL queries.
In this article, we’ll explain the syntax and practical use cases of the SPLIT_PART function in PostgreSQL, illustrating its utility with examples. Let’s go deep into how to use SPLIT_PART to enhance our data-handling skills.
What is PostgreSQL SPLIT_PART Function?
The SPLIT_PART function in PostgreSQL is designed to split a text string into parts based on a specified delimiter and then retrieve a specific part by its position. This function is especially useful for extracting structured data, like individual date components or elements from CSV-like fields, in a simple, readable way. By using SPLIT_PART, we can easily break down and access parts of a string for data extraction and transformation tasks.
Syntax
SPLIT_PART(string, delimiter, position)
Key Terms
- String Argument: The
string
argument is the input string that you want to split.
- Delimiter: The
delimiter
is a string used to define the points at which the input string should be split.
- Position: The
position
argument specifies which part of the split string should be returned. It must be a positive integer, with 1
representing the first substring.
Key Benefits of Using SPLIT_PART in PostgreSQL
- Efficient Data Extraction: Ideal for breaking down complex strings.
- Improves Query Readability: Simplifies SQL queries by reducing the need for complex string manipulations.
- Versatile Application: Can be used across SELECT, WHERE, and other clauses to make queries more dynamic.
PostgreSQL SPLIT_PART Function Examples
Let us take a look at some of the examples of the SPLIT_PART Function in PostgreSQL to better understand how this function can simplify string manipulation and data extraction tasks.
Example 1: Extracting Year and Month from Payment Date
The below query uses the SPLIT_PART() function to return the year and month of the 'payment_date' from the 'payment' table of the sample database, ie, dvdrental:

Query:
SELECT
split_part(payment_date::TEXT, '-', 1) y,
split_part(payment_date::TEXT, '-', 2) m,
amount
FROM
payment;
Output

Explanation:
The query returns the year and month along with the payment amount for each record in the 'payment'
table.
'payment_date::TEXT'
converts the 'payment_date'
to a text string.
'SPLIT_PART(payment_date::TEXT, '-', 1)'
extracts the year (the first part of the date).
'SPLIT_PART(payment_date::TEXT, '-', 2)'
extracts the month (the second part of the date).
Example 2: Splitting a Comma-Separated String
Through the below query the string 'A, B, C' is split on the comma delimiter (, ) and results in 3 substrings: ‘A’, ‘B’, and ‘C’. Because the position is 2, the function returns the 2nd substring which is ‘B’:
Query:
SELECT SPLIT_PART('A, B, C', ', ', 2);
Output

Explanation:
- The input string
'A, B, C'
is split into three substrings: '
A
'
, '
B
'
, and '
C
'
.
- The function returns the second substring, which is
'
B
'
.
Important Points About PostgreSQL SPLIT_PART Function
- If the specified position exceeds the number of available substrings, the function returns an empty string.
- Consider edge cases, such as strings without the delimiter or strings where the delimiter appears multiple times consecutively.
- The
position
argument must be a positive integer. If the position is less than 1
, PostgreSQL will return an error.
- The function can implicitly convert other data types to text. For instance, a date can be converted to text using '
::TEXT'
to use with SPLIT_PART()
.
Conclusion
The SPLIT_PART function in PostgreSQL is essential for splitting and extracting parts of strings, making it highly effective in scenarios where structured text needs to be parsed and analyzed. From splitting dates to extracting domains from email addresses, SPLIT_PART provides flexibility and simplicity in data manipulation.
Similar Reads
SQL Tutorial Structured Query Language (SQL) is the standard language used to interact with relational databases. Whether you want to create, delete, update or read data, SQL provides the structure and commands to perform these operations. SQL is widely supported across various database systems like MySQL, Oracl
8 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Window Functions in SQL SQL window functions are essential for advanced data analysis and database management. It is a type of function that allows us to perform calculations across a specific set of rows related to the current row. These calculations happen within a defined window of data and they are particularly useful
6 min read
Top 60 DBMS Interview Questions with Answers for 2025 A Database Management System (DBMS) is the backbone of modern data storage and management. Understanding DBMS concepts is critical for anyone looking to work with databases. Whether you're preparing for your first job in database management or advancing in your career, being well-prepared for a DBMS
15+ min read
SQL Exercises : SQL Practice with Solution for Beginners and Experienced SQL (Structured Query Language) is a powerful and flexible tool for managing and manipulating relational databases. Regardless of our experience level, practising SQL exercises is essential for improving our skills. Regular practice not only enhances our understanding of SQL concepts but also builds
15+ min read
SQL Cheat Sheet ( Basic to Advanced) Creating and managing databases in SQL involves various commands and concepts that handle the structuring, querying, and manipulation of data. In this guide, we will see a comprehensive cheat sheet for essential SQL operations, offering a practical reference for tasks ranging from database creation
15 min read
SQL Views Views in SQL are a type of virtual table that simplifies how users interact with data across one or more tables. Unlike traditional tables, a view in SQL does not store data on disk; instead, it dynamically retrieves data based on a pre-defined query each time itâs accessed. SQL views are particular
7 min read
MySQL Tutorial This MySQL Tutorial is made for both beginners and experienced professionals. Whether you're starting with MYSQL basics or diving into advanced concepts, this free tutorial is the ideal guide to help you learn and understand MYSQL, no matter your skill level. From setting up your database to perform
11 min read
Indexing in Databases - Set 1 Indexing is a crucial technique used in databases to optimize data retrieval operations. It improves query performance by minimizing disk I/O operations, thus reducing the time it takes to locate and access data. Essentially, indexing allows the database management system (DBMS) to locate data more
8 min read