pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joe Conway <mail@joeconway.com>
To: Chris Faulkner <chrisf@oramap.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: length of array
Date: Wed, 27 Aug 2003 17:40:17 -0700
Message-ID: <3F4D4F71.1030706@joeconway.com> (raw)
In-Reply-To: <DGENKIKMJILAAKJGFHKFCEMNCGAA.chrisf@oramap.com>
References: <DGENKIKMJILAAKJGFHKFCEMNCGAA.chrisf@oramap.com>

Chris Faulkner wrote:
> Is there a function in postgres to return the length of an array field ? I
> have seen array_dims(array) but this returns a character value. Ideally, I'd
> like something numeric returned.
> 

Afraid not in 7.3.x or earlier. In 7.3.x (but not earlier) you *can* do 
this (for a one-dimensional array at least):

SELECT
   replace(split_part(array_dims(array_fld),':',1),'[','')::int
   as low
FROM tbl;

SELECT
   replace(split_part(array_dims(array_fld),':',2),']','')::int
   as high
FROM tbl;


In 7.4 (now in beta) there are two new functions, array_lower() and 
array_upper() that do what you're looking for:

regression=# select array_lower(array_fld, 1) from tbl;
  array_lower
-------------
            1
(1 row)

regression=# select array_upper(array_fld, 1) from tbl;
  array_upper
-------------
            2
(1 row)

See the following links for more on 7.4's array support:
http://developer.postgresql.org/docs/postgres/arrays.html
http://developer.postgresql.org/docs/postgres/functions-array.html
http://developer.postgresql.org/docs/postgres/sql-expressions.html#SQL-SYNTAX-ARRAY-CONSTRUCTORS

HTH,

Joe






view thread (4+ messages)  latest in thread

Message-ID: <3F4D4F71.1030706@joeconway.com>
Permalink:  ../3F4D4F71.1030706@joeconway.com/
Also on:    postgresql.org/message-id/3F4D4F71.1030706@joeconway.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: mail@joeconway.com, chrisf@oramap.com
  Subject: Re: length of array
  In-Reply-To: <3F4D4F71.1030706@joeconway.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox