Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL ORDER chars numerically

I have a column of numbers stored as chars. When I do a ORDER BY for this column I get the following:

100
131
200
21
30
31000
etc.

How can I order these chars numerically? Do I need to convert something or is there already an SQL command or function for this?

Thank You.

like image 846
T.T.T. Avatar asked Jan 28 '09 23:01

T.T.T.


People also ask

How do I order chronologically in SQL?

The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.


1 Answers

Try this:

ORDER BY CAST(thecolumn AS int) 
like image 106
Ray Hidayat Avatar answered Sep 23 '22 15:09

Ray Hidayat