The json solution is very elegant. Just for fun, this solution using regexp (much uglier):
WITH r AS (SELECT row('quotes, "commas", and a line break".',null,null,'"fourth,field"')::text AS r) --WITH r AS (SELECT row('',null,null,'')::text AS r) --WITH r AS (SELECT row(0,1)::text AS r) SELECT CASE WHEN rr ~ '^\("",' THEN '' WHEN rr ~ '^\("' THEN regexp_replace(regexp_replace(regexp_replace(right(rr, -2), '""', '\"', 'g'), '([^\\])",.*', '\1'), '\\"', '"', 'g') ELSE (regexp_matches(right(rr, -1), '^[^,]*'))[1] END FROM r
When converting a string to text, PostgreSQL uses CSV formatting. I could not find any tools to import the cited CSV into an array, so the above is a crude text manipulation using mostly regular expressions. Maybe someone will find this useful!
Ziggy Crueltyfree Zeitgeister
source share