How to sort a varchar field as numeric field in MySQL

How to sort a varchar field as numeric field in MySQL

How to sort a varchar field as integer field in MySQL

Sorting varchar field numerically in MySQL

MySQL provide the facility to sort the column in the SQL statement but some time we take field as a varchar and store integer or float values in that column, now when you sort it then it will sort it as alphabetically because we took that field type as varchar. now we need to sort it as numerically because we store the value in that field are integer or float.

we can do this using the type casting and update your SQL statement as below

SELECT * FROM table_name ORDER BY CAST(field_name as SIGNED INTEGER) ASC

Above statement sort your field as numerically even its varchar type, so this way you can sort string as numerically or sort string as number or sort string as numeric.

Some time solution is around us but we are not able to get it.