Re: How to count the number of items in an Array?

From: Andre Lopes <lopes80andre(at)gmail(dot)com>
To: Andreas Kretschmer <akretschmer(at)spamfence(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: How to count the number of items in an Array?
Date: 2010-02-21 14:05:24
Message-ID: 18f98e681002210605n1b36ffb7j969fa8aa0269393a@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

Thanks for the reply.

My problem is that I can't have the array with all items. I need to read
from a table and store in an Array the values.

I have this:

[code]
CREATE OR REPLACE FUNCTION "public"."apr_update_newsletter_distritos"
("pEMAIL" varchar, "pID_WEBSITE_RECOLHA" varchar) RETURNS void AS
$body$
DECLARE
pEMAIL alias for $1; -- vai ser preciso
pID_WEBSITE_RECOLHA alias for $2; -- vai ser preciso
vDISTRITOS_NA_TABELA varchar[];
vDISTRITOS_NA_TABELA_CONST varchar[];
vd integer;
vas varchar;
BEGIN
-- ### Vou gravar num array os registos da chave em q se vai mexer
-- vou entao passar os registos para o array vDISTRITOS_NA_TABELA
FOR vDISTRITOS_NA_TABELA IN
SELECT array[id_distrito] FROM am_newsletter_distritos
WHERE email = pEMAIL and id_website_recolha = pID_WEBSITE_RECOLHA
LOOP
RAISE NOTICE 'value: %', vDISTRITOS_NA_TABELA;
END LOOP;

-- array 2 string
select array_to_string(vDISTRITOS_NA_TABELA, ',') into vas;
RAISE NOTICE 'string with items of array: %', vas;

-- Vou contar o número dos registos do array
-- select array_upper(vDISTRITOS_NA_TABELA, 1) into vd;
-- RAISE NOTICE 'num array: %', vd;
END;
$body$
LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;
[/code]
This select returns me 4 values:
[code]
SELECT array[id_distrito] FROM am_newsletter_distritos
WHERE email = pEMAIL and id_website_recolha = pID_WEBSITE_RECOLHA
[/code]

The values are '11, 12,16, 16'. I haven't found a way to store this as an
array to the variable vDISTRITOS_NA_TABELA. Someone can give me a clue on
how to do that?

Best Regards,

On Sun, Feb 21, 2010 at 8:38 AM, Andreas Kretschmer <
akretschmer(at)spamfence(dot)net> wrote:

> Andre Lopes <lopes80andre(at)gmail(dot)com> wrote:
>
> > Hi,
> >
> > I have googled, but I can't find how to count the number of items in an
> Array
> > in Plpgsql.
> >
> > Someone can give me a clue on that?
>
> Sure, you can use array_upper() - array_lower():
>
> Zeit: 0,205 ms
> test=*# select array_upper(array[1,2,3],1) - array_lower(array[1,2,3],1);
> ?column?
> ----------
> 2
> (1 Zeile)
>
>
> or:
>
> test=*# select array_upper(array[1,2,3],1) - array_lower(array[1,2,3],1) +
> 1;
> ?column?
> ----------
> 3
> (1 Zeile)
>
>
> Or, if your array starts with the first element, simple:
>
> test=*# select array_upper(array[1,2,3,NULL,5],1);
> array_upper
> -------------
> 5
> (1 Zeile)
>
>
>
> Andreas
> --
> Really, I'm not out to destroy Microsoft. That will just be a completely
> unintentional side effect. (Linus Torvalds)
> "If I was god, I would recompile penguin with --enable-fly." (unknown)
> Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tatsuo Ishii 2010-02-21 14:27:09 Re: pgpool error, pid not found!
Previous Message Andreas Kretschmer 2010-02-21 08:38:29 Re: How to count the number of items in an Array?