SQL ORDER chars numerically

T.T.T. picture T.T.T. · Jan 29, 2009 · Viewed 81.2k times · Source

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.

Answer

Ray Hidayat picture Ray Hidayat · Jan 29, 2009

Try this:

ORDER BY CAST(thecolumn AS int)