Re: array_dims array_lower/upper distance

Поиск
Список
Период
Сортировка
От Guy Fraser
Тема Re: array_dims array_lower/upper distance
Дата
Msg-id 1127403060.4210.45.camel@sigurd.incentre.net
обсуждение исходный текст
Ответ на Re: array_dims array_lower/upper distance  (Tony Wasson <ajwasson@gmail.com>)
Ответы Re: array_dims array_lower/upper distance  (Greg Stark <gsstark@mit.edu>)
Список pgsql-general
On Wed, 2005-21-09 at 07:48 -0700, Tony Wasson wrote:
> On 9/20/05, Matthew Peter <survivedsushi@yahoo.com> wrote:
> > Wondering if there's a way for postgres to return how
> > many elements are in a array as a single integer? For
> > instance, returning 10 (items in array) instead of
> > [-5:4]
> >
> > Also, is there a way to return the position of an item
> > in a array?
> >
>
> Try using array_upper and specify which array dimension.
>
> from http://www.postgresql.org/docs/current/static/arrays.htm:
>
> "array_dims produces a text result, which is convenient for people to
> read but perhaps not so convenient for programs. Dimensions can also
> be retrieved with array_upper and array_lower, which return the upper
> and lower bound of a specified array dimension, respectively."
>
>
> pg8=# SELECT array_dims('{4,2,3,5,6}'::INT[]);
>  array_dims
> ------------
>  [1:5]
> (1 row)
>
> pg8=# SELECT array_upper('{4,2,3,5,6}'::INT[],1);
>  array_upper
> -------------
>            5
>
So to answer his question he would likely want :

SELECT
 array_upper(item,1) - array_upper(item,0) + 1 as elements
FROM
 arraytest ;




В списке pgsql-general по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: wrong protocol sequence?
Следующее
От: Tony Wasson
Дата:
Сообщение: Re: Network Flow Schema + Bulk Import/Updates