引用这个问题:How do I grab only the latest Invoice Number
我接受了使用 MAX 函数的答案,但 Robert McKee 指出这将导致排序值,例如:
‘A’ ‘AA’ ‘AAA’ ‘AB’ ‘B’
When what I need is:
‘A’ ‘B’ ‘AA’ ‘AB’ ‘AAA’
I am trying to find a way to find the latest Version of an Invoice. The accepted answer from the referenced question will work up to a point. And it did satisfy my question... But now a new problem deserves its own question and not for me to go back and modify my original question. So…
The only thing I have to work with is the Invoice Number itself. The Invoice number has a format of #####XXX, where ##### is the actual Invoice Number and the XXX is the version number. XXX can be anywhere from ‘A’ to ‘ZZZ’.
Here is my attempt to find a plausible work around (a sql test case):
DECLARE @TempTable TABLE (MyNumber int, MyString varchar(15));
INSERT @TempTable
VALUES (100, 'A'), (100, 'AAZ'), (100, 'B'), (100, 'AZ'), (100, 'C'), (100, 'Z'), (100, 'AA'), (100, 'AB');
SELECT TOP 1
RTRIM(CAST(MyNumber AS NVARCHAR(15)) + MyString) AS InvoiceNumber
FROM @TempTable
ORDER BY RIGHT(LEFT(MyString + SPACE(2), 3), 1) DESC, RIGHT(LEFT(MyString + SPACE(2), 2), 1) DESC, LEFT(MyString, 1) DESC;
有人愿意提供更好的答案或为我指出正确的方向来清理我的吗?
提前致谢,
最佳答案
尝试这样的事情:
ORDER BY LEN(myValue),myValue
这将排序 1 个字符,然后是 2 个字符,依此类推...
关于sql - 如何对版本号进行排序,例如 ‘A’ 、 ‘B’ 、 ‘AA’ 、 ‘AB’ 和 ‘AAA’ ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33378019/