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 BAEFD3A4D95 for ; Sat, 20 Nov 2004 17:13:44 +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 78559-08 for ; Sat, 20 Nov 2004 17:13:41 +0000 (GMT) Received: from web20824.mail.yahoo.com (web20824.mail.yahoo.com [216.136.227.140]) by svr1.postgresql.org (Postfix) with SMTP id 6FE983A4D7C for ; Sat, 20 Nov 2004 17:13:40 +0000 (GMT) Received: (qmail 11402 invoked by uid 60001); 20 Nov 2004 17:13:39 -0000 Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; b=LN7l+41w2Ha1yM+fgs3TxQAqx6Bu4N5AFDBga8aUrW1BAM2+ET5iGXOKKAk9jx0P9ey+fKqV/kr4hGtjWXjEiE2p9XBSgQZi7JQ2YaabtyxCiCXAqH0tRxeOcRwtbkbH28dUfJ/wM80LDdC6bUL9p8OCb+7fjPEtO4okz81zI4M= ; Message-ID: <20041120171339.11400.qmail@web20824.mail.yahoo.com> Received: from [24.252.83.152] by web20824.mail.yahoo.com via HTTP; Sat, 20 Nov 2004 09:13:39 PST Date: Sat, 20 Nov 2004 09:13:39 -0800 (PST) From: Jeff Eckermann Subject: Re: How to get the max on a char column? To: grupos@carvalhaes.net, pgsql-sql@postgresql.org In-Reply-To: <419F5A83.1040504@carvalhaes.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii 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/214 X-Sequence-Number: 19533 --- 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