我购买的数据库中包含非零填充的字母数字高尔夫球场“数字”,如ZZ-1,ZZ-2,ZZ-9,ZZ-10 ......
我需要编写一个查询,它将拉出MAX数值,当在字符串上使用时,MAX不是DWIM,并在ZZ-10上将ZZ-9排序为MAX。
SELECT MAX( CourseNumber ) AS x
FROM courses
WHERE CourseNumber
RLIKE 'ZZ'
仅选择数字时会出现同样的问题:
SELECT MAX(SUBSTR(CourseNumber, 4)) AS x
FROM courses WHERE CourseNumber RLIKE 'ZZ'
任何人都有聪明的方法吗?我认为它必须涉及SUBSTR,但我想不出如何使它工作。
答案 0 :(得分:4)
SELECT MAX(
CAST(
SUBSTRING(CourseNumber, 4) AS UNSIGNED)
)
FROM courses WHERE CourseNumber RLIKE 'ZZ'
答案 1 :(得分:1)
也许to_number(SUBSTR(coursenumber,3))