ORDER BY (numbers and letters)

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default ORDER BY (numbers and letters)

    I've got a table of data that I want to sort by lot number. The lots are in the
    form of 1A, 1B, 2A and so on. Obviously, this is a text field. So when I use
    ORDER BY I get all the first digits grouped together, and so on, which I don't
    want.

    How does one sort a hibrid of numbers and letters so that you get the expected
    results?

    Glen T Guest

  2. Similar Questions and Discussions

    1. SQL: Order by text & numbers in same field
      Hi I have a problem, don't think there's an easy solution but here goes: I have a db text field that contains text and numbers. I need to sort my...
    2. regexp needed to split letters from numbers
      Hi -- On Sat, 27 Sep 2003, Thomas A. Reilly wrote: Just to add to the list of ideas: in 1.8.0 you can use scanf: irb(main):016:0> require...
    3. Numbers or letters in a TextBox
      How I can have a TextBox that to only lets me enter letters or numbers?
    4. sql SORT order not working on numbers?
      Hello all... I'm using asp to get records from an access database, very similar to the way datagrid would work. The title of each column in my table...
    5. List numbers in sequential order
      Update YourTable SET YourNewCol = (SELECT COUNT(*) + 1 FROM YourTable AS b WHERE b.Field1 = YourTable.Field1) -- Andrew J. Kelly SQL Server...
  3. #2

    Default Re: ORDER BY (numbers and letters)

    What are the expected results?


    Originally posted by: Glen T
    I've got a table of data that I want to sort by lot number. The lots are in
    the form of 1A, 1B, 2A and so on. Obviously, this is a text field. So when I
    use ORDER BY I get all the first digits grouped together, and so on, which I
    don't want.

    How does one sort a hibrid of numbers and letters so that you get the expected
    results?



    Dan Bracuk Guest

  4. #3

    Default Re: ORDER BY (numbers and letters)

    Well, if the numeric portion of the lot numbers were padded with zeroes, they
    would sort correctly:

    001A
    002A
    013A

    ... instead of ...

    13A
    1A
    2A


    Or if the lot number is only one level deep [number]+[letter] you could
    probably separate the value into number + alpha columns and then sort by:
    lotNumber, lotLetter




    mxstu Guest

  5. #4

    Default Re: ORDER BY (numbers and letters)

    The lot numbers are actual lot numbers, used as addresses on a strata-title
    vacation property. So, I'd be hesitant to pad them with zeros.

    I'd like them to come out like this:

    1A
    1B
    2A
    2B
    ...
    9A
    9B
    10A
    10B
    11A
    ...
    33B

    Instead, what I'm getting is this:
    10A
    10B
    ...
    19A
    19B
    1A
    1B
    20A
    20B
    ...

    Glen T Guest

  6. #5

    Default Re: ORDER BY (numbers and letters)

    What are the rules for the lot numbers? Are they always a single number +
    letter code ? Or is possible they can vary? If they will never vary from that
    format, you might be able to use a database view for sorting the values.

    mxstu Guest

  7. #6

    Default Re: ORDER BY (numbers and letters)

    The range is pretty simple. Starts at 1A and goes in A-B pairs to a max of 33.
    Glen T Guest

  8. #7

    Default Re: ORDER BY (numbers and letters)

    Then just create a sql view that presents a virtual view of the table plus two
    columns: the "number" and "alpha" portion of the values. Then you can sort by
    those columns instead of sorting by lotNumber ("1A", etc). That will work.

    mxstu Guest

  9. #8

    Default Re: ORDER BY (numbers and letters)

    I found the following snippet of SQL code:

    Order By Case When IsNumeric(Right(StreetAddr, 1)) = 1
    Then right('00000' + StreetAddr + ' ', 5)
    Else Right('00000' + StreetAddr, 5)
    End

    But I really don't know how to adapt this to CF. This is similar to what I
    want to do:

    [url]http://www.tek-tips.com/viewthread.cfm?qid=1132304&page=1[/url]


    Glen T Guest

  10. #9

    Default Re: ORDER BY (numbers and letters)

    I think it is even simpler in your case. If the numbers are a max of two
    digits and the letter code is only one digit.

    SELECT lotNumber
    FROM yourTable
    ORDER BY
    CAST(LEFT(lotNumber, LEN(lotNumber)-1) AS int),
    RIGHT(lotNumber, 1)

    Although, I really recommend putting this in a "view". It will make your code
    much cleaner.

    mxstu Guest

  11. #10

    Default Re: ORDER BY (numbers and letters)

    Hmm.. on second look at the example from the link your posted, you could
    probably reduce it to this

    SELECT lotNumber
    FROM yourTable
    ORDER BY RIGHT('00000' + lotNumber, 3)

    The reason being that your "lotNumbers" always have an alpha character at the
    end. The poster in the link did not.


    mxstu Guest

  12. #11

    Default Re: ORDER BY (numbers and letters)

    Here's the complete query, but I'm getting missing parameter error on the ORDER
    BY clause:

    SELECT fld_ID, fld_lotNumber, fld_lastName1, fld_firstName1, fld_lastName2,
    fld_firstName2, fld_lastName3, fld_firstName3, fld_email1, fld_email2
    FROM tbl_members
    ORDER BY CAST(LEFT(fld_lotNumber, LEN(fld_lotNumber)-1) AS int),
    RIGHT(fld_lotNumber, 1)

    Glen T Guest

  13. #12

    Default Re: ORDER BY (numbers and letters)

    What database are you using?

    Did you see my last post? That modification of the code you found is even simpler.
    mxstu Guest

  14. #13

    Default Re: ORDER BY (numbers and letters)

    Yes, this works just right:

    SELECT lotNumber
    FROM yourTable
    ORDER BY RIGHT('00000' + lotNumber, 3)

    Thanks a bunch, mstu!
    Glen T Guest

  15. #14

    Default Re: ORDER BY (numbers and letters)

    I'm using Access, so that probably explains the missing parameter issue.
    Glen T Guest

  16. #15

    Default Re: ORDER BY (numbers and letters)

    This brings up an interesting point that I hadn't really considered before
    (because I'm a database virgin). That is, I didn't realize that I was actually
    operating Access functions via CF / SQL. I kind of assumed that an Access file
    was just data structured in a certain format, and that the functions lived in
    the server side of CF.

    Glen T Guest

  17. #16

    Default Re: ORDER BY (numbers and letters)

    Yes, it is an important distinction. Take these two queries

    <!--- example 1 --->
    <cfquery .....>
    SELECT NOW() AS CurrentDateAndTime
    </cfquery>

    <!--- example 2 --->
    <cfquery .....>
    SELECT #NOW()# AS CurrentDateAndTime
    </cfquery>

    They look similar, but the "example 1" uses the Access database funtion named
    "now()" and "example 2" uses the cf function named "now()". Although they both
    do essentially the same thing. Kind of trippy, huh? ;-)

    mxstu Guest

  18. #17

    Default Re: ORDER BY (numbers and letters)

    More than trippy, it is a bit distressing. Means that this stuff is even less
    portable than I thought it would be. You have juggle database-specific stuff
    along with the application platform-specific stuff and the html development
    platform-specific stuff. I guess you really are painting yourself into a corner
    when you commit to a platform.

    Glen T Guest

  19. #18

    Default Re: ORDER BY (numbers and letters)

    Well.. if you're looking for portable, I wouldn't use Access. I don't know
    about the newer versions, but the earlier versions did not conform to some of
    the generally accepted sql standards.

    With most databases, if you stick to the ansi standards your code can be
    relatively portable. However, the other school of thought is that you may as
    well take advantage of the software you have. Why bother sinking money into a
    product if you don't use many of it's capabilities.


    mxstu Guest

  20. #19

    Default Re: ORDER BY (numbers and letters)

    Originally posted by: mxstu
    Well.. if you're looking for portability, I wouldn't use Access. I don't know
    about the newer versions, but the earlier versions did not conform to some of
    the generally accepted sql standards.

    With most databases, if you stick to the ansi standards your code can be
    relatively portable. However, the other school of thought is that you may as
    well take advantage of the software you have. Why bother sinking money into a
    product if you don't use many of it's capabilities.
    I agree with you to some extent. But sometimes, you don't have a choice. For
    example, right now I'm doing a small job for a client whose ISP only supports
    Access.

    What I have to keep in mind is that I will not necessarily be able to reuse
    work I've done for others without modification. But then, that's probably the
    way of the world. And I agree that there is no point in failing to exploit the
    full capabilities of the tools you are currently using. I just wish that I
    could count on taking those tools with me from one job to the next. Wishful
    thinking, of course...



    Glen T Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139