Jump to content

Order By with letters and numbers?


Petty_Crim

Recommended Posts

I'm trying to make a query that orders descending on a field that has a mix of letters and numbers. The fields are like this E-(a number) ie E-1. The problem is though when it gets to E-10 it put that ahead of E-9, it seems to treat E-10 as E-1.

 

It should go like this:

E-10

E-9

E-8

so on

 

atm its doing

E-9

E-8

...

E-1

E-10

Anyone know how to get around this?

Link to comment
Share on other sites

Your problem exists because your table column is of type VARCHAR, and in ASCII and language terms E-1(anything) comes before E-9.

It works letter by letter  (because you're working with characters NOT numbers).

Character1 | Character2 | Comparision
=============================
E              | E              | EQUAL
-              | -               | EQUAL
1               | 9              | 1 is LESS than 9 , now we have our ordering...

 

The answer to your problem is to segregate your field into two fields.

A char field (myCharField) and an int field (myIntField). In the CHAR field you have "E" and in the INT field you have 10 or 9 or whatever number you fancy. Then you can do ORDER BY myCharField, myIntField

Then you will have your ordering correctly.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.