tags:

views:

123

answers:

1

I have a varchar field in my table and I want to sort it. But I need to handle this field as integer. Meaning if sort as text the order is "19,2,20" but I want to get the right order "2,19,20".

Can anyone help me?

+1  A: 
SELECT  *
FROM    mytable
ORDER BY
        CAST (mycol AS DECIMAL)
Quassnoi
thanks Quassnoi
ntan
@Quassnoi: Just wondering, but is CASTing more efficient than ORDER BY mycol+0
dnagirl
nice idea i test drive it
ntan
`@dnagirl`: `DECIMAL` will silently truncate everything beyond the `[0-9]` range, `mycol + 0` (which implies `CAST AS DOUBLE`) will issue a warning. As for performance, I think they're identical.
Quassnoi