how to get the max data using natural sort?
-
20-08-2019 - |
Question
10.12.1
10.12.10
10.12.11
10.12.12
10.12.13
10.12.14
10.12.15
10.12.16
10.12.17
10.12.18
10.12.19
10.12.2
10.12.3
10.12.4
10.12.5
10.12.6
10.12.7
10.12.8
10.12.9
This is a varchar column. But '10.12.19' is the max data i need. How can i get it?
Solution
SQL code
ORDER BY CAST(SUBSTRING_INDEX(col,'.',-1) AS SIGNED) DESC;
OTHER TIPS
MySQL doesn't have a natural sort yet. It's been an open feature request since 2003: http://bugs.mysql.com/bug.php?id=1588
I'd do it outside of SQL, or change the values (or add a new column) so that the numbers are fixed width.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow