2

I have a purchased database with non-zero-padded alphanumeric golf course "numbers" like ZZ-1, ZZ-2, ZZ-9, ZZ-10...

I need to write a query which will pull the MAX numeric value and MAX when used on a string does not DWIM and sorts ZZ-9 as MAX over ZZ-10.

SELECT MAX( CourseNumber ) AS x
FROM courses
WHERE CourseNumber
RLIKE 'ZZ'

Same problem when only selecting the digits:

SELECT MAX(SUBSTR(CourseNumber, 4)) AS x 
FROM courses WHERE CourseNumber RLIKE 'ZZ'

Anyone have a clever way to do this? I was think it must involve SUBSTR but I couldn't think how to make it work.

2
  • Are the alpha prefixes fixed-length, or are there 'PDQ' and 'ABCDFEGFGHIJLFERWEFDSFDS' prefixes as well? Either way, if you're doing a lot of searching/matching on those sub-fields, you may want to invest the time to pre-split those bits out into separate fields so you can do direct matching on those derived fields, otherwise your where clauses are going to turn into a hideous mess of substring/cast clauses. Commented Mar 10, 2011 at 20:07
  • At present they are all 2 letters, hyphen, numeral. The people my boss bought this data from are idiots though so it may not stay that way forever but a solution assuming that pattern works for me! Commented Mar 10, 2011 at 20:12

2 Answers 2

4
SELECT MAX(
  CAST(
    SUBSTRING(CourseNumber, 4) AS UNSIGNED)
  ) 
FROM courses WHERE CourseNumber RLIKE 'ZZ'
Sign up to request clarification or add additional context in comments.

Comments

1

maybe to_number(SUBSTR(coursenumber,3))

4 Comments

mySQL doesn't have to_number; I believe that's an Oracle function.
@jerry - yep im an Oracle guy at heart :)
Maybe something like SELECT CAST(SUBSTR(CourseNumber), 3) AS UNSIGNED) FROM courses WHERE CourseNumber RLIKE 'ZZ'
Randy - Googling to_number() set me on the right path! Thanks! forums.mysql.com/read.php?10,152446,233605#msg-233605

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.