gpt4 book ai didi

sql-server - 当字符串包含空格时 SQL order by 的奇怪行为

转载 作者:行者123 更新时间:2023-12-03 00:12:04 27 4
gpt4 key购买 nike

SQL 示例 1:

SELECT TestField 
FROM (VALUES('Ne'), ('NE')) AS TestTable(TestField)
ORDER BY TestField COLLATE Latin1_General_CS_AS

结果1:

Ne  
NE

SQL示例2(NE和a之间有2个空格,而Ne和a之间只有1个):

SELECT TestField 
FROM (VALUES('Ne a'), ('NE a')) AS TestTable(TestField)
ORDER BY TestField COLLATE Latin1_General_CS_AS

结果2:

NE  a  
Ne a

谁能解释一下吗?

谢谢

最佳答案

第二个查询:

SELECT TestField 
FROM (VALUES
('Ne a'),
('NE a')
-- 12345
) AS TestTable(TestField)
ORDER BY TestField COLLATE Latin1_General_CS_AS

对于区分大小写的排序规则,按字母顺序生成的差异(位置 4: <a)比按大小写顺序生成的差异(位置 2: e <E)。因此 NE a 位于 Ne a 之前。

另一个例子:a(位置 2)之间的区别比大小写顺序更重要(位置 1:e E):

SELECT '{' + TestField  + '}'
FROM (VALUES
('ea'),
('E ') -- or ('E')
-- 12
) AS TestTable(TestField)
ORDER BY TestField COLLATE Latin1_General_CS_AS
/*
TestField
---------
{E }
{ea}
*/

有关 Rusanu 的 blog 的更多详细信息.

更新#1:您可以使用SQL_EBCDIC037_CP1_CS_AS排序规则:

SELECT TestField 
FROM (VALUES
('Ne a'),
('NE a')
-- 12345
) AS TestTable(TestField)
ORDER BY TestField COLLATE SQL_EBCDIC037_CP1_CS_AS
/*
TestField
---------
Ne a
NE a
*/

但是这种排序规则可能会产生一些奇怪结果。

示例:

SELECT  x.ColA AS ColA_Latin1_General_CS_AS
FROM (
SELECT 'A'
UNION ALL
SELECT 'AB'
UNION ALL
SELECT 'ABC'
UNION ALL
SELECT 'zzzz'
) x(ColA)
ORDER BY x.ColA COLLATE Latin1_General_CS_AS
/*
ColA_Latin1_General_CS_AS
----------------------------
A
AB
ABC
zzzz
*/

对比

SELECT  x.ColA AS ColA_SQL_EBCDIC037_CP1_CS_AS
FROM (
SELECT 'A'
UNION ALL
SELECT 'AB'
UNION ALL
SELECT 'ABC'
UNION ALL
SELECT 'zzzz'
) x(ColA)
ORDER BY x.ColA COLLATE SQL_EBCDIC037_CP1_CS_AS
/*
ColA_SQL_EBCDIC037_CP1_CS_AS
----------------------------
zzzz
A
AB
ABC
*/

注意:我从未使用过 SQL_EBCDIC037_CP1_CS_AS 排序规则,因此我不推荐。

更新#2:文本值分为两列(或多列)

-- Scenario #1: before/during insert/update, spaces are trimmed with LTRIM
SELECT TestField1 F1, TestFiel2 AS F2
FROM (VALUES
('JOHN', 'ZOE'),
('JOHN', 'Albano')
) AS TestTable(TestField1, TestFiel2)
ORDER BY TestField1 COLLATE Latin1_General_CS_AS, TestFiel2 COLLATE Latin1_General_CS_AS
/*
F1 F2
---- ------
JOHN Albano
JOHN ZOE
*/
-- Scenario #2: during insert/update spaces are not trimmed (with LTRIM)
SELECT LTRIM(TestField1) COLLATE Latin1_General_CS_AS AS F1, LTRIM(TestFiel2) COLLATE Latin1_General_CS_AS AS F2
FROM (VALUES
('JOHN', ' ZOE'), -- 1 extra space
('JOHN', 'Albano')
) AS TestTable(TestField1, TestFiel2)
ORDER BY F1, F2
/*
F1 F2
---- ------
JOHN Albano
JOHN ZOE
*/

注意:我将使用场景#1中描述的解决方案。

关于sql-server - 当字符串包含空格时 SQL order by 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28997747/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com