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

Поиск
Список
Период
Сортировка
От Andreas Kretschmer
Тема Re: How to count the number of items in an Array?
Дата
Msg-id 20100221083829.GA9280@tux
обсуждение исходный текст
Ответ на How to count the number of items in an Array?  (Andre Lopes <lopes80andre@gmail.com>)
Ответы Re: How to count the number of items in an Array?  (Andre Lopes <lopes80andre@gmail.com>)
Список 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 по дате отправления:

Предыдущее
От: Pavel Stehule
Дата:
Сообщение: Re: Asynchronous queries - processing listen (notify) in a procedural language
Следующее
От: Andre Lopes
Дата:
Сообщение: Re: How to count the number of items in an Array?