Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting a VARCHAR column as FLOAT using the CAST operator don't work in MySQL [closed]

Tags:

casting

mysql

I can't find a way to sort a varchar column casted as float. Here is my SQL request:

SELECT guid, number FROM table ORDER BY 'CAST(number AS FLOAT) DESC'

The "number" column is defined like this:

number  varchar(20) ascii_general_ci

And the values defined in this column for my test are :

0.00
200.00
20.00
100.00

MySQL totally ignore the CAST operator and sort the columns by guid...

Is there a bug in MySQL or did I do something wrong ?

like image 633
Nicolas BADIA Avatar asked May 17 '12 10:05

Nicolas BADIA


1 Answers

Try this trick (helps to sort strings as numbers)-

SELECT guid, number FROM table ORDER BY number * 1 DESC

It will help MySQL to cast string to number.


Another solution -

...CAST(value as DECIMAL(10,5))
like image 178
Devart Avatar answered Oct 18 '22 13:10

Devart