Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with max() in sql server

Tags:

sql

sql-server

I have alphanumeric values like. XYZ1,XYZ2......XYZ11, XYZ12 and so on, now I want to select only the Max numeric value, i.e. 12 here. I tried-

 select max(REPLACE(ID,'XYZ','')) from myTable;

but this is returning 9. why?

like image 472
Wondering Avatar asked Mar 23 '26 10:03

Wondering


1 Answers

Try converting to INT before max

select max(cast(REPLACE(ID,'XYZ','') as int)) from myTable;
like image 112
NullRef Avatar answered Mar 25 '26 00:03

NullRef



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!