Introduction
Sometimes, you have a list of words or values in one string and want to work with each item separately. Other times, you have a list and want to join it back into one string.
SELECT string_to_array(text, delimiter) AS array_result; SELECT array_to_string(array, delimiter) AS string_result;
SELECT string_to_array('apple,banana,cherry', ',');
SELECT array_to_string(ARRAY['red', 'green', 'blue'], '-');
SELECT string_to_array('', ',');
SELECT array_to_string(ARRAY[]::text[], ',');
WITH example AS ( SELECT 'dog,cat,bird' AS animals_string, ARRAY['lion', 'tiger', 'bear'] AS animals_array ) SELECT string_to_array(animals_string, ',') AS animals_array_from_string, array_to_string(animals_array, ',') AS animals_string_from_array FROM example;
string_to_array('apple,banana,cherry', ',') return?ARRAY['red','green','blue'] into a string separated by semicolons in PostgreSQL?SELECT array_to_string(string_to_array('dog|cat|bird', '|'), ', ');SELECT array_to_string(string_to_array('one,two,three'), ',');tags storing comma-separated strings like 'red,blue,green'. You want to find rows where the tag 'blue' exists. Which query correctly uses string_to_array and array_to_string to help filter these rows?Pass the test and this pattern moves unknown