Re: [HACKERS] string_to_array with empty input

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: "David E(dot) Wheeler" <david(at)kineticode(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 17:42:03
Message-ID: 603c8f070904021042p766a5062ke876891e6bf948fa@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

On Thu, Apr 2, 2009 at 12:10 PM, David E. Wheeler <david(at)kineticode(dot)com> wrote:
> 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;

Ah! Thanks for the tip.

...Robert

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Scott Marlowe 2009-04-02 17:48:35 Re: [GENERAL] Re: [GENERAL] ERROR: XX001: could not read block 2354 of relation…
Previous Message Robert Haas 2009-04-02 17:41:21 Re: [HACKERS] string_to_array with empty input

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2009-04-02 17:42:08 Re: global index - work in progress
Previous Message Robert Haas 2009-04-02 17:41:21 Re: [HACKERS] string_to_array with empty input