我正在处理两个查询,根据字符串中包含的字符查找一些记录,但要求两个不同的字符,我得到相同的结果。我不明白为什么。查询是:
select * from qc.qualitycuit (nolock) where fullname LIKE '%'+CHAR(208)+'%'
select * from qc.qualitycuit (nolock) where fullname LIKE '%'+CHAR(240)+'%'
其中 char(240)=ð和char(208)=Ð
我已经阅读了一些关于类似功能的帖子,但仍无法找到解释。
答案 0 :(得分:1)
char()
函数会产生以下值:
select char(208)
= Ð or LATIN CAPITAL LETTER ETH
select char(240)
= ð or LATIN SMALL LETTER ETH
这是同一拉丁字母的两个不同案例。默认排序规则不区分大小写,因此在LIKE
子句中使用这些字符之一将产生相同的结果。
例如,在默认排序规则中,此查询会生成Equal
:
select
char(208),
char(240),
case when char(208) = char(240) then 'Equal' else 'Not Equal' end
此外,以下查询中的LIKE
会在不区分大小写的排序规则下返回相同的结果:
if object_id('tempdb..#Test') is not null
drop table #Test
create table #Test (TestVal nvarchar(20) primary key clustered)
insert into #Test
values
('1' + CHAR(208)),
('2' + CHAR(240))
select null as [1], * from #Test
where TestVal like '%' + CHAR(208) + '%' -- returns both rows
select null as [2], * from #Test
where TestVal like '%' + CHAR(240) + '%' -- returns both rows
如果将排序规则更改为区分大小写,则查询将返回一行:
if object_id('tempdb..#Test') is not null
drop table #Test
create table #Test
(TestVal nvarchar(20) collate SQL_Latin1_General_CP1255_CS_AS primary key clustered)
insert into #Test
values
('1' + CHAR(208)),
('2' + CHAR(240))
select null as [1], * from #Test
where TestVal like '%' + CHAR(208) + '%' -- returns 1st row
select null as [2], * from #Test
where TestVal like '%' + CHAR(240) + '%' -- returns 2nd row
请注意,您可以浏览排序规则以查找哪些区分大小写:
select *
FROM sys.fn_helpcollations()
where description like '%case-sensitive%'