Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old November 23rd, 2005, 01:35 AM
Robert Fitzpatrick
Guest
 
Posts: n/a
Default 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@postgresql.org)

  #2  
Old November 23rd, 2005, 01:36 AM
terry@ashtonwoodshomes.com
Guest
 
Posts: n/a
Default Re: Sorting varchar w/single digits

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@greatgulfhomes.com
Fax: (416) 441-9085

[color=blue]
> -----Original Message-----
> From: pgsql-general-owner@postgresql.org
> [mailto:pgsql-general-owner@postgresql.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@postgresql.org)
>[/color]


---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

  #3  
Old November 23rd, 2005, 01:36 AM
Michael Fuhr
Guest
 
Posts: n/a
Default Re: Sorting varchar w/single digits

On Tue, Aug 10, 2004 at 08:54:45PM -0400, Robert Fitzpatrick wrote:[color=blue]
> 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?[/color]

Try something like this:

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

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

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles