X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 659469FA2DB for ; Mon, 5 Jun 2006 11:49:11 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 91742-04 for ; Mon, 5 Jun 2006 11:49:02 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from presinet.com (mail.presinet.com [209.53.156.1]) by postgresql.org (Postfix) with ESMTP id 5235E9FA111 for ; Mon, 5 Jun 2006 11:49:01 -0300 (ADT) Received: from [10.10.1.151] ([10.10.1.151]) by presinet.com with Microsoft SMTPSVC(6.0.3790.1830); Mon, 5 Jun 2006 07:46:20 -0700 Message-ID: <4484445E.2000006@presinet.com> Date: Mon, 05 Jun 2006 07:49:02 -0700 From: Bricklen Anderson User-Agent: Thunderbird 1.5.0.2 (X11/20060522) MIME-Version: 1.0 To: Mauro Bertoli CC: pgsql-sql@postgresql.org Subject: Re: Get max value from an comma separated string References: <20060529112238.61079.qmail@web27009.mail.ukl.yahoo.com> In-Reply-To: <20060529112238.61079.qmail@web27009.mail.ukl.yahoo.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 05 Jun 2006 14:46:21.0000 (UTC) FILETIME=[CF491C80:01C688AE] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/53 X-Sequence-Number: 25289 Mauro Bertoli wrote: > Hi, I've a field that contain values-comma-separated > like > A) 1;2;3;;5 -- ;2;;4;5 > but also > B) 12;34;18 > how I can get the max value? > For A I tried: > SELECT max(array_upper(string_to_array(answer,';'),1)) > FROM values; > and work fine, but for B case I don't find a solution > like > SELECT max(string_to_array(answer,';')) FROM values; > > Any ideas? > Thanks for any hint > You could try rearranging the values into rows, like so: CREATE OR REPLACE FUNCTION text2rows (TEXT,TEXT) RETURNS SETOF TEXT AS $$ SELECT (string_to_array($1, $2))[x.i] FROM generate_series(1,array_upper(string_to_array($1,$2),1)) AS x(i); $$ language sql strict; select max(val) from (SELECT text2rows(answer,';') as val FROM answer) as t;