为什么“œ”在NVarchar中匹配'oe'而在Varchar中不匹配

时间:2018-05-04 18:57:01

标签: sql-server tsql replace

SELECT REPLACE(N'Chloe', 'œ', 'o'), REPLACE('Chloe', 'œ', 'o')

结果:

Chlo    Chloe

这太奇怪了。

另一种方式:

SELECT
  CASE WHEN N'œ' = N'oe' THEN 1 ELSE 0 END as NVarcharMatch,
  CASE WHEN 'œ' = 'oe' THEN 1 ELSE 0 END as VarcharMatch

结果:

NVarCharMatch   VarcharMatch
1   0

1 个答案:

答案 0 :(得分:2)

遗留SQL排序规则(“SQL”排序规则前缀)和二进制排序规则(“BIN”前缀一次仅比较单个字符,因此“œ”永远不能等于“oe”。

Windows排序规则和Unicode比较使用更强大的比较规则。这允许单个“œ”字符比较等于2个连续字符“oe”,因为它们在语义上是相同的。

--Chlo because Unicode comparison equal
SELECT REPLACE(N'Chloe' COLLATE SQL_Latin1_General_CP1_CI_AS, 'œ', 'o'); 

--Chloe because legacy SQL comparison unequal
SELECT REPLACE('Chloe' COLLATE SQL_Latin1_General_CP1_CI_AS, 'œ', 'o'); 

--Chloe because binary comparison unequal
SELECT REPLACE('Chloe' COLLATE Latin1_General_BIN, 'œ', 'o');

--Chlo because Windows collation comparison equal
SELECT REPLACE('Chloe' COLLATE Latin1_General_CI_AS, 'œ', 'o');