Skip to content
Advertisement

How to Select Max Numeric from Varchar Field in MYSQL

Here I want to select the maximum number (which is 3) from the first number.

But, that’s not what I want. The output I want is 3 without the dot at both side of the number. I want the fourth value of the

Advertisement

Answer

You can use SUBSTRING_INDEX to extract the first number from each userid string, and then take the MAX of those:

Note that you need to CAST the value to an integer to ensure that e.g. 11 sorts higher than 3.

Output

Demo on SQLFiddle

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement