【问题标题】:SQL LIKE not returning correct resultSQL LIKE 没有返回正确的结果
【发布时间】:2012-10-29 18:09:27
【问题描述】:

我有一个包含 LastName 和 FirstName 的 Person 表。

考虑以下记录

FirstName == 'Pete' 和 LastName == 'Aaaa'

当我运行以下选择语句时:

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 Fiddle 中运行它时 - 每次选择都会返回 1 条记录,因为它应该 SQL Fiddle query

有人有什么想法吗?

【问题讨论】:

  • 虽然我不明白为什么它会影响您所描述的事情,但您是否在进行不区分大小写的比较?
  • 您能提供您的 Person 表架构吗?可能有排序问题
  • 什么是列数据类型?该行的SELECT CAST(LastName AS VARBINARY(100)) 的结果是什么?这是您正在运行的确切查询,还是您出于问题的目的对其进行了更改?
  • 我的经验表明你很可能做错了什么。可能like 子句已损坏(例如,在调用之间使用了空格,或者空间敏感性已被激活,或者记录已被调用之间的其他代码更改(例如测试用例的设置/拆卸代码) )。
  • @camillajac - Ah aa 在丹麦语/挪威语中被特别视为Å

标签: sql sql-server-2008 tsql sql-like


【解决方案1】:

试试这个

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%'

【讨论】:

  • 谢谢,但现在我必须了解如何在 Linq 中执行此操作;因为 Linq 不支持排序规则 (?)
猜你喜欢
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 2014-09-13
  • 2012-08-01
  • 2014-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多