Question

I've a table that has a column with data type 'Memo'.
This field contains values like "X003", "X0022", "X002", "X0030", "X0031", "X004". In short the first "X00" part is common, and the rest part is a integer.
I want to select that row which contains the largest integer in the second part via a sql query. How should i write the sql?
If it was only a integer, I would use "MAX()" in sql to get the maximum value. But it's different from that. What should I do? Please help me.

Was it helpful?

Solution

SELECT max(val(mid([col],3))) FROM thetable;

I.e. Select the maximum of the value after the 3rd character, cast to a number (or 0 if not convertible).

OTHER TIPS

You can do a simple MID() in your query to get the last two characters and then convert them to integer or even do a MAX() directly on the chunk.

How should i write the sql?

If you are looking for sql query

SELECT 
    MAX(CONVERT(INT,CASE WHEN PatIndex('%[a-z]%',REVERSE(memo)) > 0
      THEN RIGHT(memo,PatIndex('%[a-z]%',REVERSE(memo))-1)
      ELSE NULL END)) AS maxmemo
FROM Table1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top