Re: CSV hack

From: david(at)fetter(dot)org (David Fetter)
To: andrew(at)dunslane(dot)net (Andrew Dunstan), dbdpg-general(at)svr3(dot)postgresql(dot)org, pgsql-hackers(at)postgresql(dot)org
Subject: Re: CSV hack
Date: 2003-12-11 07:43:34
Message-ID: 200312110743.hBB7hYqa003302@fetter.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

In article <3FD7EC2A(dot)6020903(at)dunslane(dot)net> you wrote:
>
> You also need to quote values containing the separator.

Roight! Thanks for the heads-up :) And now, version 2.

Cheers,
D

CREATE OR REPLACE FUNCTION csv(anyarray) RETURNS TEXT AS
'DECLARE
in_array ALIAS FOR $1;
temp_string TEXT;
quoted_string TEXT;
i INTEGER;
BEGIN
FOR i IN array_lower(in_array, 1)..array_upper(in_array, 1)
LOOP
IF in_array[i]::TEXT ~ ''[,"]''
THEN
temp_string := ''"'' || replace(in_array[i]::TEXT, ''"'', ''""'') || ''"'';
ELSE
temp_string := in_array[i]::TEXT;
END IF;
IF i = array_lower(in_array, 1)
THEN
quoted_string := temp_string;
ELSE
quoted_string := quoted_string || '','' || temp_string;
END IF;
END LOOP;
RETURN quoted_string;
END;
' LANGUAGE 'plpgsql';
--
David Fetter david(at)fetter(dot)org http://fetter.org/
phone: +1 510 893 6100 cell: +1 415 235 3778

When a man tells you that he got rich through hard work, ask him:
'Whose?'
Don Marquis, quoted in Edward Anthony, O Rare Don Marquis

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Dave Page 2003-12-11 11:09:01 7.4 build problem on Linux Vserver
Previous Message Shridhar Daithankar 2003-12-11 06:50:08 Re: PostgreSQL port to pure Java?