Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL-Server is ignoring my COLLATION when I'm using LIKE operator

I'm working with Spanish database so when I'm looking for and "aeiou" I can also get "áéíóú" or "AEIOU" or "ÁÉÍÓÚ", in a where clause like this:

SELECT * FROM myTable WHERE stringData like '%perez%'

I'm expencting:

* perez
* PEREZ
* Pérez
* PÉREZ

So I changed my database to collation: Modern_Spanish_CI_AI

And I get only:

* perez
* PEREZ

But if I do:

SELECT * FROM myTable WHERE stringData like '%perez%' COLLATE Modern_Spanish_CI_AI 

I get all results OK, so my question is, why if my database is COLLATE Modern_Spanish_CI_AI I have to set the same collation to my query???

I'm using SQL-Server 2008

like image 212
Santiago Avatar asked Dec 09 '22 15:12

Santiago


1 Answers

You can use COLLATE, eg.

SELECT * 
FROM TableName
WHERE strData COLLATE Latin1_general_CI_AI = 'perez' COLLATE Latin1_general_CI_AI

both sides must have the same collation.

  • SQLFiddle Demo
  • SQLFiddle Demo (using LIKE)

Others:

  • Selecting a SQL Server Collation
like image 79
John Woo Avatar answered Apr 23 '23 08:04

John Woo