Microsoft SQL Server does not return everything when using LIKE clause

眉间皱痕 提交于 2019-12-11 10:38:43

问题


I have a Products table in SQL Server 2014 Express with records in it.

A few product names (records) are the following:

  • Test product
  • Teszt termék
  • Teszt termék 2

When I execute the following query, everything works just fine:

SELECT * 
FROM Products
WHERE name LIKE 'te%'

It retrieves all three records. However, when I use

SELECT * 
FROM Products
WHERE name LIKE 'tes%'

is executed, only "Test product" is retrieved. And when the query is

SELECT * 
FROM Products
WHERE name LIKE 'tesz%'

then it works again, it fetches all the records starting with "Tesz".

Any idea what the problem might be?

Thanks for your help in advance.


回答1:


I downloaded your backup.

The database collation is set to Hungarian_CI_AS.

Sz is treated as a letter in its own right in Hungarian (the thirty-second letter of the Hungarian alphabet). If you do not want this behaviour then you need to avoid Hungarian collations and it should work. (I tested all SQL Server 2014 collations with names ending CI_AS and the only ones showing the behaviour are Hungarian_CI_AS, Hungarian_100_CI_AS, SQL_Hungarian_CP1250_CI_AS)

You can do this either by altering the column collation (preferable) or by adding an explicit COLLATE clause to the query (less efficient and will block queries such as WHERE name LIKE 'tes%' being resolved through an index seek).

You might also consider changing the database collation but this will not cascade through to pre-existing table columns created on the old collation.



来源:https://stackoverflow.com/questions/29474460/microsoft-sql-server-does-not-return-everything-when-using-like-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!