2012-11-09 66 views
3

我有一个姓氏和名字的人表。SQL LIKE没有返回正确的结果

考虑下面的记录,其中

姓== '彼得' 和姓氏== 'AAAA'

当我运行下面的SELECT语句:

select * from Person where FirstName='Pete' and LastName like 'a%' -- 0 record returned 
select * from Person where FirstName='Pete' and LastName like 'aa%' -- 1 record returned 
select * from Person where FirstName='Pete' and LastName like 'aaa%' -- 0 record returned 
select * from Person where FirstName='Pete' and LastName like 'aaaa%' -- 1 record returned 

当我在SQL小提琴中运行此操作 - 每个选择返回1条记录,因为它应该是 SQL Fiddle query

有人有什么想法吗?

+0

虽然我不明白为什么它应该影响事情像你描述的,你在干什么不区分大小写的比较? – ChrisF

+1

你能否提供你的Person表格模式?可能有排序问题 –

+0

什么是列数据类型?那行的SELECT CAST(LastName AS VARBINARY(100))是什么结果?这是你正在运行的确切查询还是你为了这个问题而改变了它? –

回答

2

试试这个

select * from Person where FirstName='Pete' and LastName collate Latin1_General_CI_AS like 'a%' 
select * from Person where FirstName='Pete' and LastName collate Latin1_General_CI_AS like 'aa%' 
select * from Person where FirstName='Pete' and LastName collate Latin1_General_CI_AS like 'aaa%' 
select * from Person where FirstName='Pete' and LastName collate Latin1_General_CI_AS like 'aaaa%' 
+0

谢谢,但现在我必须找出如何在Linq中执行此操作;因为Linq不支持整理(?) – camillajac