Sunday, November 21, 2010

Order By with VARCHAR

Hi everyone,

In order by clause of varchar(50) column for numeric values,the result is different from order by with integer column..

E.g. select * from classroom order by classroom (where classroom is varchar(max) column)
the result is:
       1
       10
       11
       12
        2
        3
      ........
now suppose i need it order by like 1 2 3 4 5 ... i need to convert the column to integer , instead of changing the design of table you could write your query as this...

select * from classroom order by cast(classroom as integer)

would give you result as 1 2 3 4 5........

Happy Coding,
Regards,
Krunal Panchal

1 comment: