Communities for your favorite technologies. Explore all Collectives
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams
Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
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?
'XYZ9' < 'XYZ12'
Try converting to INT before max
max
select max(cast(REPLACE(ID,'XYZ','') as int)) from myTable;
Add a comment
It's still treating your value as a string instead of a number. Try:
select max(CAST(REPLACE(ID,'XYZ','') AS INT) from myTable;
Because you're still comparing strings. The fact that they contain only numeric digits doesn't mean that they're not strings. You need to convert them:
SELECT MAX(CAST(REPLACE(id, 'XYZ', '') AS INT)) FROM My_Table
Another method is
select max(REPLACE(ID,'XYZ','')*1) from myTable
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
'XYZ9' < 'XYZ12'?