我想在 sql 服务器中找到最大数量我有字符串部分和整数部分的值

I want to find maximum number in sql server i have values with string part and integer part

我有像 SP-1,SP-2,SP-3.....SP-10,SP-11.

这样的值

我必须在我的 SQL 服务器

中从此处获取最大数量
SELECT MAX(SUBSTRING(SupplementId,4,10)) AS max_num FROM supreg

当我执行代码时,我得到的最大数字是 9。

尝试:

SELECT MAX(CAST(SUBSTRING(SupplementId,4,10) AS INT)) AS max_num FROM supreg

SELECT MAX(CAST(REPLACE(SupplementId,'SP-','') AS INT)) AS max_num FROM supreg

另一种方法是使用长度:

SELECT TOP 1 sr.*
FROM supreg sr
ORDER BY length(SupplementId) DESC, SupplementId DESC;

这样您就可以得到具有最大值的整行。

根据您的要求,您可以使用

 Select Max(substr ( tb_simple.sampleValues ,4  )  ) as maxValue from tb_simple