In article <[EMAIL PROTECTED]> 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 [EMAIL PROTECTED] 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

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Reply via email to