How do I perform an accent insensitive compare (e with è, é, ê and ë) in SQL Server?

放肆的年华 提交于 2019-12-17 02:10:50

问题


I'm looking to compare two varchars in SQL, one would be something like Cafe and the other Café is there a way in SQL that will allow the two values to be compared. For instance:

SELECT *
FROM Venue
WHERE Name Like '%cafe%'

So if there is a venue with the name Big Bobs Café Extraordinaire it would be included in the result set?


回答1:


Coerce to an accent insensitive collation

You'll also need to ensure both side have the same collation to avoid errors or further coercions if you want to compare against a table variable or temp table varchar column

and because the constant value will have the collation of the database Update: only for local variables, not for constants nope, not even then

SELECT *
FROM Venue
WHERE
   Name COLLATE Latin1_general_CI_AI Like '%cafe%' COLLATE Latin1_general_CI_AI



回答2:


By applying a specific accent insensitive collation to your select:

SELECT * 
FROM Venue 
WHERE Name COLLATE Latin1_General_CI_AI Like '%cafe%' COLLATE Latin1_General_CI_AI

The CI stands for "Case Insensitive" and AI for "Accent Insensitive".




回答3:


Accent Sensitive and Accent Insensitive searching can be don by using Latin1_general_CI_AI

Here AI is for Accent Insensitive and AS for Accent Sensitive ie, Café and Cafe are the same if Accent Insensitive.

In the below query Latin1_general_CI_AI can be break down into the following parts.

  • latin1 makes the server treat strings using charset latin 1, basically ascii.

  • CI specifies case-insensitive, so "ABC" equals to "abc".

  • AI specifies accent-insensitive,so 'ü' equals to 'u'.

Your query should be as follows:

SELECT * FROM Venue WHERE Name COLLATE Latin1_general_CI_AI Like '%cafe%' COLLATE Latin1_general_CI_AI

Expected Result is as follows:

 Id  name
 1  Café
 2  Cafe



回答4:


if 'A' COLLATE SQL_Latin1_General_CP1_CS_AS= 'a' COLLATE SQL_Latin1_General_CP1_CS_AS SELECT N'Yes' ELSE SELECT N'No'



来源:https://stackoverflow.com/questions/2461522/how-do-i-perform-an-accent-insensitive-compare-e-with-%c3%a8-%c3%a9-%c3%aa-and-%c3%ab-in-sql-ser

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