![]() |
VOOZH | about |
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.
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 is the input string that you want to split.delimiter is a string used to define the points at which the input string should be split.position argument specifies which part of the split string should be returned. It must be a positive integer, with 1 representing the first substring.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.
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:
π Sample DatabaseQuery:
SELECT
split_part(payment_date::TEXT, '-', 1) y,
split_part(payment_date::TEXT, '-', 2) m,
amount
FROM
payment;
Output
π PostgreSQL SPLIT_PART Function ExampleExplanation:
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).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
π PostgreSQL SPLIT_PART Function ExampleExplanation:
'A, B, C' is split into three substrings: 'A', 'B', and 'C'.'B'.position argument must be a positive integer. If the position is less than 1, PostgreSQL will return an error.::TEXT' to use with SPLIT_PART().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.