X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id CEEBED87B2 for ; Wed, 19 Oct 2005 22:01:26 -0300 (ADT) 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 13238-04 for ; Thu, 20 Oct 2005 01:01:15 +0000 (GMT) Received: from smtp103.sbc.mail.mud.yahoo.com (smtp103.sbc.mail.mud.yahoo.com [68.142.198.202]) by svr1.postgresql.org (Postfix) with SMTP id DB034D87A2 for ; Wed, 19 Oct 2005 22:01:16 -0300 (ADT) Received: (qmail 10992 invoked from network); 20 Oct 2005 01:01:20 -0000 Received: from unknown (HELO ?127.0.0.1?) (bryce?nesbitt@64.168.153.241 with plain) by smtp103.sbc.mail.mud.yahoo.com with SMTP; 20 Oct 2005 01:01:20 -0000 Message-ID: <4356EC5D.4080706@obviously.com> Date: Wed, 19 Oct 2005 18:01:17 -0700 From: Bryce W Nesbitt User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.8) Gecko/20050511 X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: casting character varying to integer - order by numeric sort Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 required=5 tests=[none] X-Spam-Level: X-Archive-Number: 200510/229 X-Sequence-Number: 23082 How can I force a character field to sort as a numeric field? I've got something like this: Postgres=> SELECT username,last_name FROM eg_member ORDER BY username; ----------+----------- 0120 | Foley 1 | Sullivan 10 | Guest 11 | User (5 rows) (I can't change the field type). I tried: SELECT username,last_name FROM eg_member ORDER BY username::integer; But postgres 7 rejects this with "ERROR: cannot cast type character varying to integer". Is there a way to force numeric sort order? I tried a variety of functions, such as to_char() and convert() without any luck. Thanks for your insight!