3

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?

2
  • Do you also expect 'XYZ9' < 'XYZ12'? Commented Jun 23, 2011 at 17:00
  • It's my bad, I should have noticed it's a string Commented Jun 23, 2011 at 17:10

4 Answers 4

6

Try converting to INT before max

select max(cast(REPLACE(ID,'XYZ','') as int)) from myTable;
Sign up to request clarification or add additional context in comments.

Comments

2

It's still treating your value as a string instead of a number. Try:

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

Comments

0

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

Comments

0

Another method is

select max(REPLACE(ID,'XYZ','')*1) from myTable

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.