tags:

views:

33

answers:

2

I have a sql statement I am using a simple sort such as the following

Select numbers
From theTable
Order By numbers

What I get in return is the following

1
11
12
14
2
21
22
23
3
35
37
etc...

I want it to be ordered in normal order

1
2
3
4
5
6
etc...
A: 

what's the datatype of the column storing the numbers? Convert/cast it to an int and you should get what you expect.

Ian Jacobs
+3  A: 

The column you are selecting isn't stored as a numeric value. You need to cast it to a some kind of number before orderby will behave the way you want.

It should be as easy as:

select numbers from order orderby cast(numbers as int)

As long as long as all the values in that column cast properly.

Justin Niessner
The second one should work, as long as no actual text characters snuck into the Numbers field.
LittleBobbyTables
order by won't work on renamed column. The second example will work, if he has only number values in his column.
hgulyan
@LittleBobbyTables: If a letter comes in then 1 10 11 2 21 is the correct order anyway.
Robert Koritnik
@Robert Koritnik: Yes, my point was that a CAST won't work in the order by if there is at least one letter in the Numbers column because then CAST will throw an error
LittleBobbyTables
@LittleBobbyTables: And my point was that if the column permitted such non-numeric values, ordering it by numerical representation of its value doesn't make any sense any more.
Robert Koritnik