SQL SERVER – Cannot Resolve Collation Conflict For Equal to Operation

Cannot resolve collation conflict for equal to operation.

SQL SERVER - Cannot Resolve Collation Conflict For Equal to Operation collationconflict-800x206

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:

  1. Where clauses
  2. Join predicates
  3. Functions
  4. 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.

Reference: Pinal Dave (https://blog.sqlauthority.com)

Best Practices, SQL Collation, SQL Error Messages, SQL Joins, SQL Scripts, SQL Server
Previous Post
SQL SERVER – Retrieve – Select Only Date Part From DateTime – Best Practice
Next Post
SQL SERVER – LEN and DATALENGTH of NULL Simple Example

Related Posts

381 Comments. Leave new

  • thank you sir, you save my day ^^

    Reply
  • Great solution from the original post… Thanks a lot

    Reply
  • Thank you very much! It is a great help!

    Reply
  • Darren Thmpson
    June 16, 2008 7:53 pm

    Cheers mate! :o)

    Reply
  • Hi have the below query and cannot quite figure out where excatly to use the collation.

    Can somebody please help:

    INSERT INTO CRMSUBSCRIPTIONSV1_HITS(USER_LOGONNAME, USER_PASSWORD, ORDER_ORDERPRODUCTID, ORDER_HITSPURCHASED)
    SELECT DISTINCT USER_LOGONNAME,USER_PASSWORD,ORDER_ORDERPRODUCTID,ORDER_HITSPURCHASED
    FROM CRMSUBSCRIPTIONSV1 WHERE CRMSUBSCRIPTIONSV1.PRODUCT_ISHITBASED = ‘1’ AND NOT EXISTS
    (SELECT USER_LOGONNAME,USER_PASSWORD,ORDER_ORDERPRODUCTID,ORDER_HITSPURCHASED
    FROM CRMSUBSCRIPTIONSV1_HITS,CRMSUBSCRIPTIONSV1 WHERE
    CRMSUBSCRIPTIONSV1.USER_LOGONNAME = CRMSUBSCRIPTIONSV1_HITS.USER_LOGONNAME AND
    CRMSUBSCRIPTIONSV1.USER_PASSWORD = CRMSUBSCRIPTIONSV1_HITS.USER_PASSWORD AND
    CRMSUBSCRIPTIONSV1.ORDER_ORDERPRODUCTID = CRMSUBSCRIPTIONSV1_HITS.ORDER_ORDERPRODUCTID AND
    CRMSUBSCRIPTIONSV1.ORDER_HITSPURCHASED = CRMSUBSCRIPTIONSV1_HITS.ORDER_HITSPURCHASED)
    GROUP BY ORDER_ORDERPRODUCTID,USER_LOGONNAME,USER_PASSWORD,ORDER_HITSPURCHASED

    Thanks in advance.

    Reply
  • Lala Ramnath
    June 21, 2008 1:08 am

    Works!!!!!!!!!!!1

    Reply
  • Thanks you very much sir. You’re the man!

    Reply
  • Great! Worked!
    Thank you very much…

    Reply
  • Thank you.

    Reply
  • Thanks Pinal!

    Reply
  • I would like to add that use Collate with ‘IN’ clause also.

    Have a look in my Query which I solved using collate for ‘IN’ clause.

    SELECT DISTINCT top 100 PERCENT FT_TBL.product, FT_TBL.RSV_desc, FT_TBL.LIST_PRICE, FT_TBL.uom, KEY_TBL.RANK, PDoc.product AS PDocProduct,
    FT_TBL.pgroup, FT_TBL.Special_Conditions, FT_TBL.shop_id, SUM([Order Contents].Qty) AS Sqty
    FROM Products1 AS FT_TBL INNER JOIN
    FREETEXTTABLE(Products1, *, ‘fuel pump’) AS KEY_TBL ON FT_TBL.ID = KEY_TBL.[KEY] LEFT OUTER JOIN
    [Order Contents] ON FT_TBL.product COLLATE DATABASE_DEFAULT = [Order Contents].[Item Number] COLLATE DATABASE_DEFAULT
    LEFT OUTER JOIN
    (SELECT product
    FROM ProductDocs
    WHERE ([Shop ID] IS NULL)) AS PDoc ON FT_TBL.product COLLATE DATABASE_DEFAULT = PDoc.product COLLATE DATABASE_DEFAULT
    WHERE (FT_TBL.shop_id IS NULL) AND (FT_TBL.car IN
    (select DISTINCT [Car Type] FROM [Car Types]))
    GROUP BY FT_TBL.product, FT_TBL.RSV_desc, FT_TBL.LIST_PRICE, FT_TBL.uom, KEY_TBL.RANK, PDoc.product, FT_TBL.pgroup, FT_TBL.Special_Conditions,
    FT_TBL.shop_id
    ORDER BY KEY_TBL.RANK DESC

    Reply
  • thanks you very much for nice tips! Its save my day. :)

    Reply
  • thanks you very much for nice tips!
    (Ovo je bila nocna mora dosad – Hvala (Serbian) )

    Reply
  • Thanks you very much, Greate.

    Reply
  • Subhrangshu Banerjee
    September 4, 2008 3:31 pm

    Thanks Pinal.
    It has helped me a lot.

    Reply
  • YES !!!! THANK YOU VERY VERY MUCH

    MERCI BEAUCOUP (French)

    MECI EN PIL (FWI Creol)

    :-D

    Reply
  • Harthikote Naveen
    September 11, 2008 5:00 pm

    Superb boss!!!!!!! thank u for ur knowledge sharing.

    Reply
  • Thank you very much!

    Reply
  • Its really works

    Thank you very much

    Reply
  • Thnaks for this Article. Really it helped me.

    Reply

Leave a Reply