0

i have a mySQL database with a text field in which is stored a number. i need to produce a recordset sorted in descending numerical order. this works fine until we get to numbers greater than 10 ie

9
8
7
6
5
4
3
2
10
1

is there a simple way of sorting this 'correctly' ? (yes, i know i should have numbers in a numerical field, but i'm working with what i have :))

i'm using the results on an asp/vbscript/jquery page so maybe even a client-side solution is viable...

any suggestions?

3 Answers 3

1
ORDER BY ABS(text_column) DESC

Or, if you also have to deal with negative values:

ORDER BY CAST(text_column AS SIGNED) DESC
Sign up to request clarification or add additional context in comments.

1 Comment

perfect solution.. thank you... didn't think it'd be that easy :)
0

You need to type cast it to INTEGER using CAST function in MySQL:

ORDER BY CAST(text_column AS UNSIGNED INTEGER)

2 Comments

You are right. It works. It doesn't if you leave the unsigned.
ya INTEGER keyword is optional.
0

Try this one -

... ORDER BY text_column * 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.