There's something fundamentally wrong with your table design. Instead of using values like '80.0 proc'
in a VARCHAR
column, you should just keep 80.0
in a column of type REAL
(or any suitable numerical type that's appropriate for your data). You could do dynamic conversion, only to be used in the ORDER BY
expression, but this is also likely to deteriorate the performance of your query.
Adding "proc" to your text here doesn't seem useful, and it will also prevent you from doing a simple conversion.
Surprisingly (see che's answer), apparently, convert(..., decimal)
is capable of ignoring the trailing rubbish. It's not something you should rely on in general, though.
The documentation on this aspect of the conversion isn't particularly clear. It's worth reading that section to be aware of the limitations of string/numbers (which would happen in general), for example:
mysql> SELECT '18015376320243459' = 18015376320243459;
-> 0
If that behaviour changed, you could probably use replace()
in this case, just to get rid of ' proc'
.
For something more complex, you could potentially use a regular expression replacement to extract the numerical value from your string and cast it into a number before sorting, but this is not supported out of the box in MySQL and that would be rather clunky anyway (fix the problem at its source: your column data type).
To deal with your legacy data, you could add an extra column and use an external program (in any language that's capable of doing a regexp replace), which shouldn't be too difficult.