Re: Sorting varchar w/single digits

From: <terry(at)ashtonwoodshomes(dot)com>
To: "'Robert Fitzpatrick'" <robert(at)webtent(dot)com>, "'PostgreSQL'" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Sorting varchar w/single digits
Date: 2004-08-11 02:39:58
Message-ID: 003601c47f4c$7f1c0000$2766f30a@development.greatgulfhomes.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

If your string *always* begins with a numeral, this will work:
ORDER BY to_number(text_field, text(99999999)), text_field

If it doesn't always begin with a numeral, you have to ensure that it does, so a textcat of zero
ensure it does...
ORDER BY to_number(textcat('0', text_field), text(99999999)), text_field

That works provided your number is never negative, (a reasonable assumption I think).

Terry Fielder
Manager Software Development and Deployment
Great Gulf Homes / Ashton Woods Homes
terry(at)greatgulfhomes(dot)com
Fax: (416) 441-9085

> -----Original Message-----
> From: pgsql-general-owner(at)postgresql(dot)org
> [mailto:pgsql-general-owner(at)postgresql(dot)org]On Behalf Of Robert
> Fitzpatrick
> Sent: Tuesday, August 10, 2004 8:55 PM
> To: PostgreSQL
> Subject: [GENERAL] Sorting varchar w/single digits
>
>
> 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?
>
> --
> Robert
>
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
> (send "unregister YourEmailAddressHere" to
> majordomo(at)postgresql(dot)org)
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Scott Marlowe 2004-08-11 02:43:01 Re: Replication options?
Previous Message Michael Fuhr 2004-08-11 01:54:57 Re: Sorting varchar w/single digits