Good afternoon! Here I fulfill request:

SELECT `DocNom`,`DocDate`, `DocName`, SUBSTRING_INDEX(`DocFile`, ':', -1) AS `FileSizeSort` FROM `Message2002` ORDER BY `FileSizeSort` DESC 

The query displays the drain and sorted naturally as a string. I would like to bring it to a number and sort it as a number. This fragment does not work as it should:

  SELECT `DocNom`,`DocDate`, `DocName`, SUBSTRING_INDEX(`DocFile`+0, ':', -1) AS `FileSizeSort` FROM `Message2002` ORDER BY `FileSizeSort` DESC 

What could be the problem? Thank!

  • Why do you add the argument, and not the result of the function? - Visman
  • Thank! Understood ... This is how it works: SELECT DocNom , DocDate , DocName , SUBSTRING_INDEX ( DocFile , ':', -1) AS FileSizeSort FROM Message2002

1 answer 1

You can explicitly use the cast function:

  SELECT `DocNom`,`DocDate`,`DocName`,SUBSTRING_INDEX(`DocFile`, ':', -1) AS `FileSizeSort` FROM `Message2002` ORDER BY CAST(`FileSizeSort` as signed) DESC 

Although your option is to add the same work with zero, if you add a zero after cutting the line: SUBSTRING_INDEX(DocFile, ':', -1) + 0

  • Thank! It works too! - rud99