Search Postgresql Archives

Re: [HACKERS] string_to_array with empty input

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

--
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux