为什么SQL Server认为CHAR(49)和CHAR(185)是相同的?

时间:2016-12-14 12:24:27

标签: sql-server ascii

为什么SQL Server认为CHAR(49)和CHAR(185)是相同的?

当将1输入NVARCHAR或TEXT类型的列时,它会将其视为CHAR(49)(一)和CHAR(185)(上标一)

示例数据:

SELECT CAST('1111' AS TEXT)  AS col1 INTO CharTest;

选择脚本:

SELECT * FROM CharTest WHERE col1 LIKE '%'+CHAR(49)+'%';
SELECT * FROM CharTest WHERE col1 LIKE '%'+CHAR(185)+'%';

两个陈述都返回:1111

1 个答案:

答案 0 :(得分:6)

不同的排序规则应该这样做

SELECT * FROM CharTest WHERE 
        col1 LIKE '%'+CHAR(49)+'%' COLLATE  SQL_Latin1_General_CP1_CS_AS;
SELECT * FROM CharTest WHERE 
        col1 LIKE '%'+CHAR(185)+'%' COLLATE SQL_Latin1_General_CP1_CS_AS;