I like function string_to_array and array_to_string. The power of these functions is unlimited. Last week somebody noticed problems with NULLs related to string_to_array function. It is true - these function doesn't handle NULL well :(.
postgres=# SELECT string_to_array('1,2,3,NULL,5',',')::int[]; ERROR: invalid input syntax for integer: "NULL"
I would to prepare new version of these function (with better support of NULL) - functions to_array and to_string. It can be prepared for 9.1.
CREATE OR REPLACE FUNCTION to_string(anyarray, sep text, nullstr text DEFAULT '') RETURNS text AS $$ SELECT array_to_string(ARRAY(SELECT coalesce(v::text, $3) FROM unnest($1) g(v)), $2) $$ LANGUAGE sql; postgres=# select to_string(array[1,2,3,4,null,5],','); to_string ------------ 1,2,3,4,,5 (1 row) postgres=# select to_string(array[1,2,3,4,null,5],',','<NULL>'); to_string ------------------ 1,2,3,4,<NULL>,5 (1 row) CREATE OR REPLACE FUNCTION to_array(text, sep text, nullstr text DEFAULT '') RETURNS text[] AS $$ SELECT ARRAY(SELECT CASE WHEN v = $3 THEN NULL::text ELSE v END FROM unnest(string_to_array($1,$2)) g(v)) $$ LANGUAGE sql; postgres=# select to_array('1,2,3,4,,5',','); to_array ------------------ {1,2,3,4,NULL,5} (1 row) postgres=# select to_array('1,2,3,4,<NULL>,5',',','<NULL>'); to_array ------------------ {1,2,3,4,NULL,5} (1 row) postgres=# select to_array('1,2,3,,5',',')::int[]; to_array ---------------- {1,2,3,NULL,5} (1 row)These SQL functions can be used for now.
Pavel
How about handling separator inside of some items? I'm about quoting or escaping of them.
ReplyDeleteI need convert to string array of unknown strings, wich can contain coma and any other separators. How I can convert it to string and back?
I'm about command like
ReplyDeleteselect string_to_array(array_to_string(ARRAY['qq','w,w'], ','), ',')
wich returns array of three items instead of two
(forgot follow-up answers)
ReplyDelete