Best practices for joining tables of different collation property database

Priya Jha 896 Reputation points
2025-01-30T10:48:40.5633333+00:00

Hi,

I am trying to join two tables of databases present on same server but with different collation property and getting following error:

CollationIssue

What are the best practices to join tables of different databases present on same server but with different collation property?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,432 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,689 questions
{count} votes

Accepted answer
  1. Erland Sommarskog 116.5K Reputation points MVP
    2025-01-30T22:40:37.0866667+00:00

    As the other say, you need cast the collation on one side, so that SQL Server knows under which rules to perform the = test.

    As for what is best practice, there are two things to consider:

    1. Correctness. That is, by which rules do you want to run the join? One collation is case-insensitive, the other is binary, that is case-sensitive.
    2. Performance. When you cast the collation of a column, any index on that column is rendered useless, since the organisation of the index depends on the collation. If one table is big and the other is small, it is better to cast the collation on the small table.

    If these two points are in conflict, the first point wins, obviously.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Nandan Hegde 33,946 Reputation points MVP
    2025-01-30T11:00:59.0333333+00:00
    1 person found this answer helpful.
    0 comments No comments

  2. Olaf Helper 45,811 Reputation points
    2025-01-30T11:14:02.4566667+00:00

    CAST the collation

    SELECT
    FROM Table1 AS T1
         INNER JOIN
         Table2 AS T2
             ON T1.Column1 COLLATE Latin1_Gereral_100_BIN2_UTF 
              = T2.Column2 COLLATE Latin1_Gereral_100_BIN2_UTF
    
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.