Postgres: String to Rows

(Last Updated On: )

In this tutorial I will show you how to convert a string to rows delimited by a character.

There are three ways to do this all lead to the same answer.

--METHOD 1
SELECT split_data FROM regexp_split_to_table('I love programming!', E' ') AS split_data;

--METHOD 2
SELECT split_data 
FROM unnest(string_to_array('I love programming!',' ')) AS split_data;

--METHOD 3
SELECT split_string.arr[i] as split_data
FROM (
	SELECT generate_series(1, array_upper(arr, 1)) AS i, arr
	FROM (SELECT ARRAY['I','love','programming!'] arr) t
) split_string;
Results:

Each executes in 11ms based on my analysis. Preferably I would go with “regexp_split_to_table”