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 42DADDA075 for ; Wed, 19 Oct 2005 22:26:13 -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 15515-08 for ; Thu, 20 Oct 2005 01:26:09 +0000 (GMT) Received: from mail.greatgulfhomes.com (mail.greatgulfhomes.com [216.191.52.66]) by svr1.postgresql.org (Postfix) with ESMTP id C92F7DA00C for ; Wed, 19 Oct 2005 22:26:10 -0300 (ADT) Received: from [10.243.102.72] ([216.191.52.74]) (authenticated bits=0) by mail.greatgulfhomes.com (8.12.8/8.12.8) with ESMTP id j9K1QCdv007921; Wed, 19 Oct 2005 21:26:13 -0400 Message-ID: <4356F419.3010105@ashtonwoodshomes.com> Date: Wed, 19 Oct 2005 21:34:17 -0400 From: Terry Fielder User-Agent: Mozilla Thunderbird 1.0.5 (Windows/20050711) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Bryce W Nesbitt Cc: pgsql-sql@postgresql.org Subject: Re: casting character varying to integer - order by numeric References: <4356EC5D.4080706@obviously.com> In-Reply-To: <4356EC5D.4080706@obviously.com> 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.016 required=5 tests=[AWL=0.016] X-Spam-Level: X-Archive-Number: 200510/230 X-Sequence-Number: 23083 Check out the function to_number() In particular here's an example... If a field named section is text containing numbers: ORDER BY to_number(t.section, text(99999999)) If the field can also contain non-numerals such as 3a, 3b, and you want 3a to show first then do this: ORDER BY to_number(t.section, text(99999999)), t.section And if the field section can actually START with an alpha, then to prevent to_number from failing do this: to_number(textcat('0', t.section), text(99999999)), t.section Terry Bryce W Nesbitt wrote: > 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! > > > ---------------------------(end of broadcast)--------------------------- > TIP 2: Don't 'kill -9' the postmaster > -- Terry Fielder terry@greatgulfhomes.com Associate Director Software Development and Deployment Great Gulf Homes / Ashton Woods Homes Fax: (416) 441-9085