Search code examples
sqlchartype-conversionsql-order-bynumerical

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.


Solution

  • Try this:

    ORDER BY CAST(thecolumn AS int)