Sub topic 11
Splitting Strings and Aggregating Rows into Text
Assume you know basic SELECT, WHERE, and GROUP BY, and understand PostgreSQL text values and NULL.
A delimited string contains several values separated by a character such as a comma. PostgreSQL provides functions for extracting one item, converting the whole string into an array, and joining array elements back into text.
Extracting items from a delimited string
split_part returns one field from a delimited string. Its positions start at 1.
SELECT split_part('red,green,blue', ',', 2) AS second_color;The result is green. If the requested position does not exist, PostgreSQL returns an empty string. If the delimiter does not occur, position 1 returns the complete string.
string_to_array converts every field into a PostgreSQL array.
SELECT string_to_array('red,green,blue', ',') AS colors;The result is {red,green,blue}. You can inspect each element by expanding the array with unnest.
SELECT color
FROM unnest(string_to_array('red,green,blue', ',')) AS color;To preserve each item's original position, use WITH ORDINALITY.
SELECT position, color
FROM unnest(string_to_array('red,green,blue', ',')) WITH ORDINALITY
AS items(color, position);This is useful when the order has meaning, such as a list of ranked tags.
Joining array elements and rows
array_to_string performs the reverse operation. It joins array elements using a separator.
SELECT array_to_string(ARRAY['red', 'green', 'blue'], ' | ') AS color_list;The result is red | green | blue.
string_agg combines values from multiple rows into one string. It is an aggregate function, so it commonly appears with GROUP BY.
WITH employees(department, name) AS (
VALUES
('Sales', 'Mina'),
('Sales', 'Omar'),
('Engineering', 'Priya')
)
SELECT department,
string_agg(name, ', ' ORDER BY name) AS members
FROM employees
GROUP BY department
ORDER BY department;The ORDER BY inside string_agg controls the order within each result. Without it, the order is not guaranteed, even if the rows often appear in an expected order.
| Function | Input | Result |
|---|---|---|
| `split_part` | One delimited string | One selected field |
| `string_to_array` | One delimited string | An array |
| `array_to_string` | An array | One delimited string |
| `string_agg` | Multiple rows | One grouped string |
NULLs and empty values
string_agg ignores NULL input values. It returns NULL when a group contains no non-NULL values. Use coalesce when an empty result is more useful:
SELECT coalesce(string_agg(name, ', ' ORDER BY name), '') AS members
FROM (VALUES (NULL::text)) AS people(name);Empty fields require care. In string_to_array('a,,c', ','), the middle field is an empty string, not NULL. Decide whether that distinction matters before processing imported data.
Exercise
Which expression returns the second value from the string 'a,b,c', and which expression joins the array ARRAY['x', 'y'] with a slash?
Show answer
Use splitpart('a,b,c', ',', 2) for the second value and arrayto_string(ARRAY['x', 'y'], '/') for x/y.