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
PostgreSQL DATE_PART Function
Handling dates and times efficiently is essential for data-driven applications, and PostgreSQL provides powerful built-in functions for managing and manipulating time-based data. One such function is the DATE_PART() function, which allows us to extract specific subfields from date and timestamp valu
5 min read
PostgreSQL- LPAD Function
The LPAD() function in PostgreSQL is a powerful tool for padding a string to the left, ensuring it reaches a specified length by filling it with a designated character or characters. This function can be particularly useful in data formatting and report generation. Let us better understand the LPAD
2 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 - LAST_VALUE Function
The PostgreSQL LAST_VALUE() function is a powerful window function used to retrieve the last value within a specified window frame of a query result set. It is particularly beneficial for performing advanced data analysis and retrieving the final value in ordered partitions. In this article, weâll e
4 min read
PostgreSQL - LAG Function
In PostgreSQL, the LAG() function is a powerful window function that allows you to access data from a previous row within the same result set. Itâs particularly useful for comparing values in the current row with values in the preceding row, making it ideal for analytical queries in PostgreSQL. For
5 min read
PostgreSQL - LEAD Function
In PostgreSQL, the LEAD() function is a powerful window function used to access a row that follows the current row at a specific physical offset. This function is generally employed to compare the value of the current row with the value of the next row following the current row. Let us better unders
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 - NTH_VALUE Function
The PostgreSQL NTH_VALUE() function is an essential tool in advanced SQL queries for analytical purposes. It allows us to retrieve the value from the nth row in an ordered set within a specified window. This functionality is invaluable when we need to pinpoint specific data points, such as the nth h
5 min read
PostgreSQL - PERCENT_RANK Function
In PostgreSQL, the PERCENT_RANK() function is used to evaluate the relative ranking of a value within a given set of values. This function is particularly useful for statistical analysis and reporting, providing insights into how values compare within a dataset. From this article, we can better unde
3 min read
PostgreSQL - POSITION Function
The PostgreSQL POSITION() function is used to find the location of a substring within a string. This function is particularly useful for text manipulation and searching operations within your database. Let us better understand the POSITION Function in PostgreSQL from this article. SyntaxPOSITION(sub
2 min read