Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
How to order mixed letter-number strings?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  1 message - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
Gordon Burditt  
View profile   Translate to Translated (View Original)
 More options 26 Feb 2007, 04:24
Newsgroups: comp.databases.mysql
From: gordonb.xf...@burditt.org (Gordon Burditt)
Date: Mon, 26 Feb 2007 04:24:35 -0000
Local: Mon 26 Feb 2007 04:24
Subject: Re: How to order mixed letter-number strings?

>I have a varchar column containing entries that I want to sort. Each
>entry consists of a combination of letters, and an optional number.

Your examples violate this, in that they seem to contain spaces, which
are neither letters nor numbers.

>Example:

>aaa
>aaa 2
>aaa 4
>aaa 12
>bbb
>bbb 4
>ccc 1
>ccc 2
>ddd
>eee

Assume this value is kept in the field `str`.

You can ORDER BY an expression, or several of them.  Your examples
don't give the full generality of the data, but let's try to do it anyway:

        left(str, 4) is the alpha part (assuming it's always 4 characters long
                including the space)
        substr(str, 5) is the numeric part as a string.
        (int)substr(str, 5) is the numeric part as a number.

so try:

        ORDER BY left(str,4), (int)substr(str,5)
        (order by the alpha part, then break ties with the numeric part,
        treated as a number)

This does NOT handle the case of no numeric part.  It also assumes that
the alpha part is fixed-length.  If you can assume there's a space in there,
you can use locate() to find the position of the space.  Stuff like if() or
ifnull() could handle the case where the numeric part comes out a zero-length
string or NULL.

        I think you can handle the no-numeric-part issue with
        if(substr(str,5) = '', '0', substr(str, 5)) as the numeric part as
                a string, defaulting to '0' if it isn't there.
        (int)if(substr(str, 5) = '', '0', substr(str, 5)) is the numeric
                part as a number.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google