英文:
Ordering text fields by letters first before numbers for all characters
问题
将文本字段按字母优先于数字进行排序,以便所有字符都按升序排列:
我正在尝试对下面的文本字段进行排序,以便它们按升序排列,字母优先显示:
B054QL9
B05KY35
B9L57F0
BBQ2T21
BYZKJ27
0043823
我目前的代码是:
SELECT
i.tclstk
FROM (
SELECT DISTINCT
tclstk
FROM Figaro.dbo.tccclw WITH(NOLOCK)
WHERE tclrqn = '6379'
AND tclbgt = 'SPC'
AND tclcom <> '0.00'
) i
ORDER BY (CASE WHEN LEFT(i.tclstk, 1) LIKE '[a-Z]' THEN 0 ELSE 1 END)
,i.tclstk
然而,这样做的效果是只考虑第一个字符,并从那里按字母顺序排序,然后根据上面的列表,它将对剩余字符的数字进行排序。
排序应该是:
BBQ2T21
BYZKJ27
B05KY35
B054QL9
B9L57F0
0043823
实际使用的数据集比上面的列表要大得多,因此上述代码需要修改以对所有七个字符进行排序。
英文:
Ordering text fields by letters first before numbers for all characters
I am trying to order the below text fields show they come out in ascending order with letters coming first;
B054QL9
B05KY35
B9L57F0
BBQ2T21
BYZKJ27
0043823
The current code I have is;
SELECT
    i.tclstk
FROM (
        SELECT DISTINCT
            tclstk
        FROM Figaro.dbo.tccclw WITH(NOLOCK)
        WHERE tclrqn = '6379'
            AND tclbgt = 'SPC'
AND tclcom <> '0.00'
    ) i
ORDER BY (CASE WHEN LEFT(i.tclstk, 1) LIKE '[a-Z]' THEN 0 ELSE 1 END)
    ,i.tclstk
However what this does is it will only look at the first character and order them in alphabetical order from there, and then as per the above list it will order them numbers first for the remaining characters.
The order should be;
BBQ2T21
BYZKJ27
B05KY35
B054QL9
B9L57F0
0043823
The actual dataset being used is much larger than the above list so the above code would need to be amended to order all seven characters.
答案1
得分: 3
SELECT
i.tclstk
FROM (
SELECT DISTINCT
tclstk
FROM Figaro.dbo.tccclw WITH(NOLOCK)
WHERE tclrqn = '6379'
AND tclbgt = 'SPC'
AND tclcom <> '0.00'
) i
ORDER BY TRANSLATE(i.tclstk, 'abcdefghijklmnopqrstuvwxyz0123456789', '0123456789abcdefghijklmnopqrstuvwxyz')
,i.tclstk
英文:
If you are using SQL SERVER 2017+, you can use this to reverse the order of numerical and letters characters.
SELECT
i.tclstk
FROM (
SELECT DISTINCT
tclstk
FROM Figaro.dbo.tccclw WITH(NOLOCK)
WHERE tclrqn = '6379'
AND tclbgt = 'SPC'
AND tclcom <> '0.00'
) i
ORDER BY TRANSLATE(i.tclstk, 'abcdefghijklmnopqrstuvwxyz0123456789', '0123456789abcdefghijklmnopqrstuvwxyz')
,i.tclstk
答案2
得分: 0
这远非美观,老实说,我建议您考虑重新处理您的数据,但它“完成了工作”。
英文:
This is far from pretty, and honestly, I would suggest that perhaps you need to reconsider your data, but it "does the job".
SELECT *
FROM (VALUES('B054QL9'),
('B05KY35'),
('B9L57F0'),
('BBQ2T21'),
('BYZKJ27'),
('0043823'))V(YourString)
ORDER BY CASE WHEN TRY_CONVERT(int,SUBSTRING(V.YourString,1,1)) IS NULL THEN 0 ELSE 1 END,
SUBSTRING(V.YourString,1,1),
CASE WHEN TRY_CONVERT(int,SUBSTRING(V.YourString,2,1)) IS NULL THEN 0 ELSE 1 END,
SUBSTRING(V.YourString,2,1),
CASE WHEN TRY_CONVERT(int,SUBSTRING(V.YourString,3,1)) IS NULL THEN 0 ELSE 1 END,
SUBSTRING(V.YourString,3,1),
CASE WHEN TRY_CONVERT(int,SUBSTRING(V.YourString,4,1)) IS NULL THEN 0 ELSE 1 END,
SUBSTRING(V.YourString,4,1),
CASE WHEN TRY_CONVERT(int,SUBSTRING(V.YourString,5,1)) IS NULL THEN 0 ELSE 1 END,
SUBSTRING(V.YourString,5,1),
CASE WHEN TRY_CONVERT(int,SUBSTRING(V.YourString,6,1)) IS NULL THEN 0 ELSE 1 END,
SUBSTRING(V.YourString,6,1),
CASE WHEN TRY_CONVERT(int,SUBSTRING(V.YourString,7,1)) IS NULL THEN 0 ELSE 1 END,
SUBSTRING(V.YourString,7,1);
答案3
得分: 0
以下是已翻译的内容:
select *
from (
VALUES (N'BBQ2T21')
, (N'BYZKJ27')
, (N'B05KY35')
, (N'B054QL9')
, (N'B9L57F0')
, (N'0043823')
) t (col1)
order by replace(replace(replace(replace(replace(replace(replace(replace(REPLACE(replace(cast(col1 as nvarchar(100)), N'0', nchar(64000)), N'1', nchar(64001)), N'2', nchar(64003)), N'3', nchar(64003)), N'4', nchar(64004)), N'5', nchar(64005)), N'6', nchar(64006)), N'7', nchar(64007)), N'8', nchar(64008)), N'9', nchar(64009))
它的功能是将数字转换为高位 Unicode 字符,应该会在常规的拉丁字符之上排序。
不过,我建议不要在家里尝试这样做,如果可能的话,最好避免这种排序方式。
英文:
My version:
select *
from (
VALUES (N'BBQ2T21')
, (N'BYZKJ27')
, (N'B05KY35')
, (N'B054QL9')
, (N'B9L57F0')
, (N'0043823')
) t (col1)
order by replace(replace(replace(replace(replace(replace(replace(replace(REPLACE(replace(cast(col1 as nvarchar(100)), N'0', nchar(64000)), N'1', nchar(64001)), N'2', nchar(64003)), N'3', nchar(64003)), N'4', nchar(64004)), N'5', nchar(64005)), N'6', nchar(64006)), N'7', nchar(64007)), N'8', nchar(64008)), N'9', nchar(64009))
What it does is to move the numbers to high plane unicode characters which should sort above the regular latin ones.
I wouldn't try this at home though, and i think you should avoid this kind of sorting if possible
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论