Saturday, May 22, 2010

Sort varchar numerically in Mysql

You may have faced this problem to sort data in ascending or descending order if datatype is varchar.
For example:
you have a table named "tablename" and a field name is "price" with data type varchar
and the values are as follows
0
100
200
50.5
150.
when you write sql like "SELECT * FROM tablename ORDER BY price ASC "
you will not be getting your result as per your query, so how will you sort your data as numerically.

Here is the way to sort data as numerically when data type is varchar
select * from `tablename` order by `fieldname` + 0 ASC


No comments:

Post a Comment