Sharon Ben-Moshe is the founder of sqlfmt, a browser-based SQL formatter and validator with misspelled-keyword suggestions across PostgreSQL, MySQL, SQLite, SQL Server, and BigQuery.
Fix "Cannot resolve collation conflict" in SQL Server and collation mismatch errors in MySQL and PostgreSQL. Master COLLATE clauses and temporary tables.
Quick answer
A SQL collation conflict occurs when a query compares, joins, or combines string columns with different collations or code pages. You resolve collation conflicts by applying explicit COLLATE clauses to matching columns (e.g. COLLATE DATABASE_DEFAULT) or configuring temporary tables to inherit user database collations.
FAQs
What does CI_AS mean in SQL Server collations?
CI stands for Case-Insensitive (A equals a). AS stands for Accent-Sensitive (e does not equal e with accent).
Why does tempdb have a different collation than my user database?
tempdb inherits the server instance collation defined when SQL Server was installed. If you restore a database with a different collation, conflicts occur when joining with #temp tables.
Does changing collation inline in a query affect performance?
Yes. Applying an inline COLLATE clause on an indexed column can force the database to convert every row, bypassing index seeks and triggering a full scan.
Can I change the collation of an entire database in SQL Server?
Yes, using ALTER DATABASE dbname COLLATE new_collation, but this does not alter existing columns in tables; you must alter individual columns separately.
Fix SQL Server Msg 245 data conversion failed errors using TRY_CAST and TRY_CONVERT. Learn defensive type casting, date parsing, and dirty data cleanup.
Fix Google BigQuery's "Unrecognized name: column" error. Learn alias visibility rules across WHERE and QUALIFY, case sensitivity, and struct unnesting.
Database migrations, joins between user tables and temporary tables, and cross-database queries frequently trigger one of the most confusing errors in database development: Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_CI_AS" in the equal to operation.
A collation defines the rules governing how textual characters are sorted and compared: character encoding, case sensitivity (CI vs CS), accent sensitivity (AI vs AS), and kana/width sensitivity. When two strings with incompatible sorting rules meet in a JOIN, WHERE, or UNION operation, database engines reject the query.
Why collation conflicts happen in SQL Server
In SQL Server, every instance has a server collation, every database has a database collation, and every text column has a column-level collation. Read our guide to SQL Server temporary tables for related tempdb interaction patterns.
The most common cause of collation conflicts is creating temporary tables in tempdb. The system database tempdb inherits the instance-level server collation. If your user database was restored with a different collation (e.g. Latin1_General_CI_AS) than the server instance collation (e.g. SQL_Latin1_General_CP1_CI_AS), joining a #temp table to a user database table fails immediately!
Simulating a SQL Server collation conflict
-- User table in database with collation: Latin1_General_CI_AS-- Temporary table created in tempdb with collation: SQL_Latin1_General_CP1_CI_ASCREATE TABLE #TempCustomers (CustomerCode VARCHAR(20));-- Joining fails with Error Msg 468:SELECT c.CustomerId, c.CustomerNameFROM Customers cJOIN #TempCustomers t ON c.CustomerCode = t.CustomerCode;-- Msg 468, Level 16, State 9:-- Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS"-- and "Latin1_General_CI_AS" in the equal to operation.
Fix 1: Using COLLATE DATABASE_DEFAULT in temporary tables
The cleanest way to prevent collation conflicts when working with temporary tables in SQL Server is to declare columns using COLLATE DATABASE_DEFAULT. This forces the temporary table column to inherit the collation of the current database rather than tempdb.
Using COLLATE DATABASE_DEFAULT
-- Safe temporary table creation: matches user database collationCREATE TABLE #TempCustomers ( CustomerCode VARCHAR(20) COLLATE DATABASE_DEFAULT NOT NULL PRIMARY KEY);-- Joins now succeed cleanly without conflict:SELECT c.CustomerId, c.CustomerNameFROM Customers cJOIN #TempCustomers t ON c.CustomerCode = t.CustomerCode;
Fix 2: Explicit query-level COLLATE casting
If you cannot alter the table schema (for example, in a cross-database join between two existing production databases), apply the COLLATE clause directly in the join condition:
Resolving collation inline in join conditions
-- Force one side of the comparison to match the other:SELECT a.AccountCode, b.BillingEmailFROM DatabaseA.dbo.Accounts aJOIN DatabaseB.dbo.BillingDetails b ON a.AccountCode = b.AccountCode COLLATE Latin1_General_CI_AS;
According to Microsoft Learn collation precedence documentation, applying an explicit COLLATE clause elevates the comparison to Explicit precedence, overriding the default implicit collations of both tables.
Collation conflicts in MySQL and PostgreSQL
Collation conflicts are not unique to SQL Server:
MySQL: Comparing utf8mb4_general_ci with utf8mb4_unicode_ci throws Error 1267 (Illegal mix of collations). Resolve by specifying COLLATE in the query or altering the table default collation as documented in the MySQL character set documentation.
PostgreSQL: Comparing columns with different ICU or libc collations throws collation mismatch error 42P21. Resolve with the COLLATE "default" or COLLATE "C" clause.
MySQL and PostgreSQL collation fixes
-- MySQL inline resolution:SELECT * FROM users uJOIN legacy_accounts a ON u.username = a.username COLLATE utf8mb4_unicode_ci;-- PostgreSQL inline resolution:SELECT * FROM contacts cJOIN directory d ON c.name = d.name COLLATE "C";
Actionable checklist for resolving collation conflicts
Add COLLATE DATABASE_DEFAULT: always specify COLLATE DATABASE_DEFAULT on character columns in SQL Server #temp tables.
Match instance and database collations: standardize server instance and user database collations during environment setup.
Audit collation mismatch in joins: check sys.columns for collation_name differences when joins fail unexpectedly.
Use utf8mb4_0900_ai_ci in MySQL: standardize on modern Unicode collations in MySQL 8 to prevent legacy utf8 mismatches.
Inspect index usage on forced collations: remember that forcing an inline COLLATE in a WHERE clause can disable index seeks.
Verify database backup restores: confirm database collations match target server settings when migrating databases across environments.
Wrapping up
Collation conflicts arise from mismatched character sorting and comparison rules across server instances, databases, and temporary storage. Applying COLLATE DATABASE_DEFAULT on temporary tables and standardizing instance collations guarantees seamless text comparisons and joins. Keep your database queries and DDL clean by formatting your SQL with sqlfmt.