pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bryce W Nesbitt <bryce1@obviously.com>
To: pgsql-sql@postgresql.org
Subject: casting character varying to integer - order by numeric sort
Date: Wed, 19 Oct 2005 18:01:17 -0700
Message-ID: <4356EC5D.4080706@obviously.com> (raw)

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!




view thread (6+ messages)  latest in thread

Message-ID: <4356EC5D.4080706@obviously.com>
Permalink:  ../4356EC5D.4080706@obviously.com/
Also on:    postgresql.org/message-id/4356EC5D.4080706@obviously.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: bryce1@obviously.com
  Subject: Re: casting character varying to integer - order by numeric sort
  In-Reply-To: <4356EC5D.4080706@obviously.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox