tags:

views:

75

answers:

3

I have a query ordered by column:

select * from mytable order by column asc — sort table

column type is varchar, so the output is:

1
10
100
11
12
13

How should I sort if I want them to sort by numeric value so the output is:

1
10
11
12
13
100

Thanks.

+10  A: 

Use:

order by cast(column as unsigned) asc
Pablo Santa Cruz
+2  A: 

This should work as well:


order by (0 + column) asc

mmattax
Very clever. Which one do you think will perform better?
Pablo Santa Cruz
I don't know the performance implications of this compared to Pablo's answer. If anyone knows, I'd like to know.
mmattax
A: 

order by cast(column as unsigned) asc

thnx very much

Dmitry
Please mark Pablo's answer as "accepted" (the check mark symbol on the left) instead of submitting an equivalent answer as a comment.
Lord Torgamus