X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id E0D30D1C9E8 for ; Wed, 24 Sep 2003 00:46:25 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 73356-08 for ; Tue, 23 Sep 2003 21:46:18 -0300 (ADT) Received: from polaris.pinpointresearch.com (66-7-238-130.cust.telepacific.net [66.7.238.130]) by svr1.postgresql.org (Postfix) with ESMTP id 29E14D1B508 for ; Tue, 23 Sep 2003 21:46:10 -0300 (ADT) Received: from betelgeuse.pinpointresearch.com (betelgeuse.pinpointresearch.com [192.168.1.179]) by polaris.pinpointresearch.com (Postfix) with ESMTP id 9A44FFE0A; Tue, 23 Sep 2003 17:46:13 -0700 (PDT) From: Steve Crawford Organization: Pinpoint Research To: Erwin Brandstetter , pgsql-admin@postgresql.org Subject: Re: Return last value in of array (PostgreSQL 7.2.1) Date: Tue, 23 Sep 2003 17:46:13 -0700 User-Agent: KMail/1.5.1 References: In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200309231746.13757.scrawford@pinpointresearch.com> X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/378 X-Sequence-Number: 10466 > I can't believe I have found a good solution here. I want to return > the last value of an array with an SQL statement... > That is what i came up with: > > select my_array_field[substr(array_dims(my_array_field), 4, 1)] > from my_table; > > But this only works, as long as the upper boundary is from 1 to 9 > (one digit). Besides it looks ugly and will perform likewise. > > Did I miss something here? > Is there any function returning more sensible data than array_dims > (returning something like '[1:4]')? IIRC I heard talk of new max and min array dimension functions in upcoming releases. I'm tired so there may be better or more elegant solutions but this one works (r is the array): r[rtrim(substring(array_dims(r), position(':' in array_dims(r))+1),']')::int] Cheers, Steve