Re: Sorting varchar w/single digits

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Robert Fitzpatrick <robert(at)webtent(dot)com>
Cc: PostgreSQL <pgsql-general(at)postgresql(dot)org>
Subject: Re: Sorting varchar w/single digits
Date: 2004-08-11 01:54:57
Message-ID: 20040811015457.GA66874@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Aug 10, 2004 at 08:54:45PM -0400, Robert Fitzpatrick wrote:
> I have varchar column with both numbers and letters, like 1 thru 10 and
> 5A thru 5G, they are unit numbers for apartments. If I have 1 thru 100,
> since it is a varchar field, it sorts like 1,10,11... instead of
> 1,2,3...
>
> Is there any way to handle this without having to make a sort order
> column?

Try something like this:

ORDER BY SUBSTRING(unitnum FROM '[0-9]+')::INTEGER, unitnum

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message terry 2004-08-11 02:39:58 Re: Sorting varchar w/single digits
Previous Message lec 2004-08-11 01:30:12 Re: Losing records when server hang