Mysql – How to get MAX numeric value from a column with char values in MySql

maxMySQL

I would like to know how can I get the max value only within the numeric values in a column with char values in a MySql database:

Supposing the column is as follows:

 col  
---------
ALL      
USD      
USD      
RET      
RET      
ACC      
ACC      
ALL      
ALL      
ERR      
ERR      
PRO      
PRO      
1        
1        
2        
2        
3        
3        
4        
4        
5        
5        
6        
6        
7        
7        
8        
8        
9        
9        
AY       
AY       
C        
C        
CF       

I should get 9 as a result of my query.

Thanks in advance.

Best Answer

If the numbers are all Integer, that you want

SELECT MAX(CAST(col AS SIGNED)) from nextg WHERE col REGEXP '^[0-9]+$';

Like @Akina said in the comments MySQL knows another way

SELECT MAX(col + 0) from nextg WHERE col REGEXP '^[0-9]+$';

See example

If you have also Decimal Numbers.

SELECT MAX(CAST(col AS DECIMAL(8,2))) from nextg WHERE col 
REGEXP '^[0-9]+\\.?[0-9]*$'