在数据库表中搜索返回不同的结果

时间:2013-12-14 15:33:33

标签: sql sql-server

我有以下SQL查询:

select Title
from dbo.posts
where CAST(CHARINDEX(N'mão', [Title]) AS int) > 0

按预期返回记录。如果我用“mao”替换“mão”,我就没有记录。

我的数据库排序规则是Latin1_General_CI_AS。我使用以下方法创建了数据库:

create database MyDb
on primary ( 
  size = 40MB,
  maxsize = 2GB,
  filegrowth = 20MB 
)
log on ( 
  size = 4MB,
  maxsize = 200MB,
  filegrowth = 2MB 
)
collate Latin1_General_CI_AS
go

可能出现什么问题?

谢谢你, 米格尔

2 个答案:

答案 0 :(得分:3)

排序Latin1_General_CI_AS是AS =重音敏感。如果你还需要获得加入mao的行,那么你需要一个Accent Insensitive排序规则。

AS / AI排序规则示例(注意:UNION删除重复项):

PRINT 'Test #1' -- AS : mão and mao are different words
SELECT N'mão' collate Latin1_General_CI_AS AS Col1 
UNION
SELECT N'mao' collate Latin1_General_CI_AS AS Col1 

PRINT 'Test #2' -- AI: mão and mao are similar words
SELECT N'mão' collate Latin1_General_CI_AI AS Col2 
UNION
SELECT N'mao' collate Latin1_General_CI_AI AS Col2 

结果:

Test #1
Col1
----
mão
mao
(2 row(s) affected)

Test #2
Col2
----
mão
(1 row(s) affected)

答案 1 :(得分:1)

这一切都与排序规则'Latin1_General_CI_AS'有关。忽略大小写,但检查重音。

因此,mão mao 这两个词是不同的。

以下是MSDN的一些参考资料。

http://technet.microsoft.com/en-us/library/ms179886.aspx

如果强制整理是强调不敏感的,则应该记录回来。

TSQL代码段:

-- Ignoring accents will return the data record
SELECT Title
FROM dbo.posts
WHERE 
    Title COLLATE Latin1_General_CP1_CI_AI 
    LIKE '%mao%' COLLATE Latin1_General_CI_AI;