Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues with case and collation when my SQL Server database is set to Latin1_General_100_CI_AI

My SQL Server 2008 database is set to Latin1_General_100_CI_AI collation, yet when I query the database using Management Studio it is still accent sensitive. What am I missing?

Also, I get the following message when joining two tables on a nvarchar. The tables are both on the same collation too.

Cannot resolve the collation conflict between "Latin1_General_100_CI_AI" and "Latin1_General_CI_AS" in the equal to operation.

Any help would be greatly appreciated.

like image 654
Andrew Avatar asked Jan 25 '11 14:01

Andrew


People also ask

Is SQL_Latin1_General_CP1_CI_AS deprecated?

The documentation recommends SQL_Latin1_General_CP1_CI_AS as the recommended code page, but that is now considered obsolete in newer versions of MS SQL Server.

What is collation sensitivity in SQL Server?

Collations in SQL Server provide sorting rules, case, and accent sensitivity properties for your data. Collations that are used with character data types, such as char and varchar, dictate the code page and corresponding characters that can be represented for that data type.

What is the default collation for SQL Server?

Default server-level collation is SQL_Latin1_General_CP1_CI_AS. If you are migrating databases from SQL Server to Managed Instance, check the server collation in the source SQL Server using SERVERPROPERTY(N'Collation') function and create a Managed Instance that matches the collation of your SQL Server.

Can you create two tables with different collation in a database?

It is absolutely possible to database with a different collation on a same SQL Server Instance. It is also possible to create an individual column in a table with different collations from server instance and database as well.


2 Answers

Try casting one of the fields into the other field's collation:

SELECT  *
FROM    as_table
JOIN    ai_table
ON      ai_field = as_field COLLATE Latin1_General_100_CI_AI

or

SELECT  *
FROM    ai_table
JOIN    as_table
ON      as_field = ai_field COLLATE Latin1_General_100_CI_AS

Note that casting a field makes the predicate unsargable against the index on this field.

like image 133
Quassnoi Avatar answered Oct 12 '22 11:10

Quassnoi


Once you've already created objects inside the database, simply changing the collation doesn't change the existing objects. You can see this by right-clicking the tables and scripting them out - they'll have lots of notes about the collations of the varchar fields. If you want the objects to change, you'll need to recreate them.

like image 44
Brent Ozar Avatar answered Oct 12 '22 11:10

Brent Ozar