Re: [HACKERS] string_to_array with empty input

From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Greg Stark <stark(at)enterprisedb(dot)com>, Sam Mason <sam(at)samason(dot)me(dot)uk>, pgsql-general(at)postgresql(dot)org, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [HACKERS] string_to_array with empty input
Date: 2009-04-02 16:10:34
Message-ID: 1AE3D3CD-EC64-43E9-ACD5-56C8A0B9D12E@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

On Apr 1, 2009, at 12:19 PM, Robert Haas wrote:

>> my @ints = map { $_ || 0 } split ',', $string;
>>
>> This ensures that I get the proper number of records in the example
>> of something like '1,2,,4'.
>
> I can't see that there's any way to do this in SQL regardless of how
> we define this operation.

It's easy enough to write a function to do it:

CREATE OR REPLACE FUNCTION trim_blanks (anyarray) RETURNS anyarray AS $$
SELECT ARRAY(
SELECT CASE WHEN $1[i] IS NULL OR $1[i] = '' THEN '0' ELSE
$1[i] END
FROM generate_series(1, array_upper($1, 1)) s(i)
ORDER BY i
);
$$ LANGUAGE SQL IMMUTABLE;

Best,

David

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David E. Wheeler 2009-04-02 16:17:21 Re: [HACKERS] string_to_array with empty input
Previous Message Sam Mason 2009-04-02 15:57:33 Re: Help with C-Function on Postgre

Browse pgsql-hackers by date

  From Date Subject
Next Message David E. Wheeler 2009-04-02 16:17:21 Re: [HACKERS] string_to_array with empty input
Previous Message K. Srinath 2009-04-02 16:01:08 questions about not-null constraints and inheritance