Search Postgresql Archives

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

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

 



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@xxxxxxxxxxxxx> wrote:
Andre Lopes <lopes80andre@xxxxxxxxx> 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@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