Cannot resolve collation conflict for equal to operation.
In MS SQL SERVER, the collation can be set at the column level. When compared to 2 different collation columns in the query, this error comes up.
SELECT ID FROM ItemsTable INNER JOIN AccountsTable WHERE ItemsTable.Collation1Col = AccountsTable.Collation2Col
If columns ItemsTable.Collation1Col and AccountsTable.Collation2Col have different collation, it will generate the error “Cannot resolve collation conflict for equal to operation“.
To resolve the collation conflict add the following keywords around “=” operator.
SELECT ID FROM ItemsTable INNER JOIN AccountsTable WHERE ItemsTable.Collation1Col COLLATE DATABASE_DEFAULT = AccountsTable.Collation2Col COLLATE DATABASE_DEFAULT
Watch this quick video for resolution:
Collation can affect the following areas:
- Where clauses
- Join predicates
- Functions
- Databases (e.g. TempDB may be in a different collation database_default than the other databases some times)
Checkout following video how collation can change the order of the column:
Just like collation, there is another problem with SQL Server and that is related to Parameter Sniffing, I have written a detailed blog post about how the parameter sniffing can be resolved in SQL Server.
Here are some really good blog posts which you can read about parameter sniffing and how you can avoid the problem related to it.
- SQL SERVER – Parameter Sniffing Simplest Example
In this blog post, we discuss what actually is parameter sniffing and how we can overcome it with the help of recompiling the stored procedure. - SQL SERVER – Parameter Sniffing and Local Variable in SP
It is easy to overcome the parameter sniffing for the stored procedure by declaring the local variable inside the stored procedure. - SQL SERVER – Parameter Sniffing and OPTIMIZE FOR UNKNOWN
You can take advantage of the new query hint of Optimize For Unknown to simulate the local variable in the stored procedure. A very underutilized technique indeed. - SQL SERVER – DATABASE SCOPED CONFIGURATION – PARAMETER SNIFFING
This new database level enhancement was introduced recently which can help you overcome any issue with the parameter sniffing. - SQL SERVER – Parameter Sniffing and OPTION (RECOMPILE)
The oldest and most traditional technique to not cache the query plans and compile your stored procedure or queries every single time to get optimal performance. - Performance and Recompiling Query – Summary
This post summarizes the entire series of parameter sniffing, performance and recompiling query.
Reference: Pinal Dave (https://blog.sqlauthority.com)
381 Comments. Leave new
Thanks for your advice with the collation error, it saved us lot of time.
Eersteklas!
Works like a charm!
Gracias, me saco del problema
Thank!!!
let me be # 27 to say thanx…
Thanks dude -you’ve made life easier!
Thanks!!!
Thanks a lot. !!!
Great help!
Googled “Cannot resolve collation conflict for equal to operation” and the first hit was this perfect solution!
Many Thx!
Hi,
I got the errros below, did anyone know about it?
Expression type int is invalid for COLLATE clause
Thanks
Thank you so much! Nice and easy solution!
Thanks!!! It Realy Works !!!
Thanks! This worked well for a database move I’m working on. I needed to concatenate a bunch of obsolete fields to dump into another new field. Here’s my example:
select
‘varchar_field1: ‘+varchar_field1+’
varchar_field2: ‘+varchar_field2+’
varchar_field3: ‘+varchar_field1+’
text_field1: ‘+cast(text_field1as varchar(800)) COLLATE DATABASE_DEFAULT
from my_table
Without collate database_default, I’d get this error:
Cannot resolve collation conflict for column 1 in SELECT statement.
Great… Going live with a major project and this little gem was a sweet, quick and perfect resolution
Cheers
Superb!!! Thank you!
hi
i too face this problem and found that the collation of one of the database is different. now i want to make the collation same. can i change the collation of an existing database and will it update collation for all existing tables and columns of that database.
Thank you,
Really appreciate it.
This work , very helpful to you
It Realy Works !!!
Thanks
Wow this works but just wondering why it works?
Thanks
Thanks a lot