Re: How to count the number of items in an Array? - Mailing list pgsql-general

From Andreas Kretschmer
Subject Re: How to count the number of items in an Array?
Date
Msg-id 20100221083829.GA9280@tux
Whole thread Raw
In response to How to count the number of items in an Array?  (Andre Lopes <lopes80andre@gmail.com>)
Responses Re: How to count the number of items in an Array?
List pgsql-general
Andre Lopes <lopes80andre@gmail.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°

pgsql-general by date:

Previous
From: Pavel Stehule
Date:
Subject: Re: Asynchronous queries - processing listen (notify) in a procedural language
Next
From: Andre Lopes
Date:
Subject: Re: How to count the number of items in an Array?