按字母优先于数字对文本字段进行排序,适用于所有字符。

huangapple go评论60阅读模式
英文:

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
&#160;&#160;&#160; i.tclstk
FROM (
&#160;&#160;&#160;&#160;&#160;&#160;&#160; SELECT DISTINCT
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; tclstk
&#160;&#160;&#160;&#160;&#160;&#160;&#160; FROM Figaro.dbo.tccclw WITH(NOLOCK)
&#160;&#160;&#160;&#160;&#160;&#160;&#160; WHERE tclrqn = &#39;6379&#39;
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AND tclbgt = &#39;SPC&#39;
			AND tclcom &lt;&gt; &#39;0.00&#39;
&#160;&#160;&#160; ) i
ORDER BY (CASE WHEN LEFT(i.tclstk, 1) LIKE &#39;[a-Z]&#39; THEN 0 ELSE 1 END)
&#160;&#160;&#160; ,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 = &#39;6379&#39;
            AND tclbgt = &#39;SPC&#39;
            AND tclcom &lt;&gt; &#39;0.00&#39;
    ) i
ORDER BY TRANSLATE(i.tclstk, &#39;abcdefghijklmnopqrstuvwxyz0123456789&#39;, &#39;0123456789abcdefghijklmnopqrstuvwxyz&#39;)
    ,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(&#39;B054QL9&#39;),
            (&#39;B05KY35&#39;),
            (&#39;B9L57F0&#39;),
            (&#39;BBQ2T21&#39;),
            (&#39;BYZKJ27&#39;),
            (&#39;0043823&#39;))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&#39;BBQ2T21&#39;)
	,	(N&#39;BYZKJ27&#39;)
	,	(N&#39;B05KY35&#39;)
	,	(N&#39;B054QL9&#39;)
	,	(N&#39;B9L57F0&#39;)
	,	(N&#39;0043823&#39;)
) t (col1)
order by replace(replace(replace(replace(replace(replace(replace(replace(REPLACE(replace(cast(col1 as nvarchar(100)), N&#39;0&#39;, nchar(64000)), N&#39;1&#39;, nchar(64001)), N&#39;2&#39;, nchar(64003)), N&#39;3&#39;, nchar(64003)), N&#39;4&#39;, nchar(64004)), N&#39;5&#39;, nchar(64005)), N&#39;6&#39;, nchar(64006)), N&#39;7&#39;, nchar(64007)), N&#39;8&#39;, nchar(64008)), N&#39;9&#39;, 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

huangapple
  • 本文由 发表于 2023年5月31日 23:04:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76374911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定