X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 79BA63A4E97 for ; Sat, 20 Nov 2004 17:52:35 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 91970-02 for ; Sat, 20 Nov 2004 17:52:32 +0000 (GMT) Received: from nitrogenio.carvalhaes.net (oxigenio.carvalhaes.net [200.102.144.130]) by svr1.postgresql.org (Postfix) with ESMTP id 1851C3A42E6 for ; Sat, 20 Nov 2004 17:52:31 +0000 (GMT) Received: from [192.168.0.5] ([192.168.0.5]) (authenticated bits=0) by nitrogenio.carvalhaes.net (8.12.11/8.12.11) with ESMTP id iAKHj5Nk001340; Sat, 20 Nov 2004 15:45:05 -0200 Message-ID: <419F68A7.70402@carvalhaes.net> Date: Sat, 20 Nov 2004 15:54:15 +0000 From: Rodrigo Carvalhaes Reply-To: grupos@carvalhaes.net User-Agent: Mozilla Thunderbird 0.9 (X11/20041103) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Jeff Eckermann Cc: pgsql-sql@postgresql.org Subject: Re: How to get the max on a char column? References: <20041120171339.11400.qmail@web20824.mail.yahoo.com> In-Reply-To: <20041120171339.11400.qmail@web20824.mail.yahoo.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Carvalhaes-MailScanner-Information: Please contact the ISP for more information X-Carvalhaes-MailScanner: Found to be clean X-MailScanner-From: grupos@carvalhaes.net X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200411/215 X-Sequence-Number: 19534 Dear Jeff, Thank you for your help. I tried : select max((id::text)::integer) from test; and works perfectly! Greetings from Brazil! Rodrigo Carvalhaes Jeff Eckermann wrote: >--- Rodrigo Carvalhaes wrote: > > > >>Hi ! >> >>I am quite confused of the results on a SELECT >>max... >> >>My environment: >>Conectiva Linux 10, PostgreSQL 7.4.6 (compiled from >>the sources) >> >>My problem is the "select max(id) FROM test" the >>result is 20 but the >>right is 1020. Is this a BUG or I am crazy ?? >> >> > >For a char() column, '20' is the maximum of the values >that you have inserted, because the comparison is >text-based, not numeric. If you want numeric sorting, >you will need to cast the value, like: > >select max(cast(id as integer)) from test; > >I'm not sure offhand whether in fact a direct cast >from char() to integer is available; you may need to >cast to "text" first. > >But if you expect to be able to sort numerically, why >are you not using a numeric datatype? > > > >>Cheers, >> >>Rodrigo Carvalhaes >> >>The SQL... >> >>teste=# CREATE TABLE test ( id char(15), name >>char(80) ); >>CREATE TABLE >>teste=# \d test >> Table "public.test" >>Column | Type | Modifiers >>--------+---------------+----------- >>id | character(15) | >>name | character(80) | >> >>teste=# INSERT INTO test VALUES ( '10', 'luidgi'); >>INSERT 15303727 1 >>teste=# INSERT INTO test VALUES ( '20', 'luis'); >>INSERT 15303728 1 >>teste=# INSERT INTO test VALUES ( '1010', 'ruan'); >>INSERT 15303729 1 >>teste=# INSERT INTO test VALUES ( '1020', 'lion'); >>INSERT 15303730 1 >>teste=# SELECT * FROM test; >> id | >> name >> >> >> >-----------------+---------------------------------------------------------------------------------- > > >>10 | luidgi >>20 | luis >>1010 | ruan >>1020 | lion >>(4 rows) >> >>teste=# SELECT max(id) FROM test; >>max >>----- >>20 >>(1 row) >> >>teste=# select max(id) FROM test; >>max >>----- >>20 >>(1 row) >> >> >> >> >> >>---------------------------(end of >>broadcast)--------------------------- >>TIP 1: subscribe and unsubscribe commands go to >>majordomo@postgresql.org >> >> >>---------------------------(end of >>broadcast)--------------------------- >>TIP 1: subscribe and unsubscribe commands go to >>majordomo@postgresql.org >> >> >> > > > > >__________________________________ >Do you Yahoo!? >Meet the all-new My Yahoo! - Try it today! >http://my.yahoo.com > > > >