Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Interesting Increase Key Value Post By CSS SQL Server EngineersSeries of "Bad habits to kick" by Aaron Bertrand »
    comments

    This was asked on twitter recently and I gave the answer there. I decided to write a blog post about this because I can use over 140 charaters here instead.
    You will see the Cannot resolve collation conflict for equal to operation error when you try to join 2 tables. let's take a look at what we need to do to resolve this. First create and populate these two tables

    1. use tempdb
    2. go
    3.  
    4.  
    5.  
    6. create table Test (SomeColumn varchar(100) not null)
    7. insert Test values('bla')
    8. insert Test values('test')
    9.  
    10.  
    11. create table Test2 (SomeColumn varchar(100) collate Traditional_Spanish_CI_AI not null)
    12. insert Test2 values('Niño')
    13. insert Test2 values('bla')

    Now run the following join between the Test and Test2 tables

    1. select * from Test t1
    2. join Test2 t2 on t1.SomeColumn = t2.SomeColumn

    Here is the error you will get

    Server: Msg 446, Level 16, State 9, Line 1
    Cannot resolve collation conflict for equal to operation.

    To quickly fix this you can use collate in your SQL, you have to make sure that the collation is the same on both columns. So you either add the collate with the collation of the column in test2 to the column in the test table or vice-versa. Here is one example

    1. select * from Test t1
    2. join Test2 t2 on t1.SomeColumn = t2.SomeColumn collate Traditional_Spanish_CI_AI

    You can also apply collate on the other column...first we need to know what the default was in your database. We can use the ANSI information_schema.columns view to get this info, we need to use the collation_name column. Run the following query to grab it

    1. select column_name,collation_name
    2. from information_schema.columns
    3. where table_name = 'test'

    In my case it is SQL_Latin1_General_CP1_CI_AS, now the query becomes the following

    1. select * from Test t1
    2. join Test2 t2 on t1.SomeColumn collate SQL_Latin1_General_CP1_CI_AS = t2.SomeColumn

    Just for fun let's see what the collation is for the column in the Test2 table

    1. select column_name,collation_name
    2. from information_schema.columns
    3. where table_name = 'Test2'

    As expected it is Traditional_Spanish_CI_AI

    You can use the ::fn_helpcollations()function which returns a list of all the collations supported by SQL Server to get more info about the collation

    Run the following query (and yes :: is not a typo)

    1. select * from ::fn_helpcollations()
    2. where name ='Traditional_Spanish_CI_AI'

    The query returns the following description for Traditional_Spanish_CI_AI'

    Traditional-Spanish, case-insensitive, accent-insensitive, kanatype-insensitive, width-insensitive

    One thing to be aware of is that when using collate it needs to do a conversion so you might get performance problems...make sure to check those execution plans.

    Hopefully this will help some person when dealing with this in the future.




    *** If you have a SQL related question try our Microsoft SQL Server Programming forum or our Microsoft SQL Server Admin forum

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    5858 views
    Instapaper

    4 comments

    Comment from: George Mastros (gmmastros) [Member]
    *****
    George Mastros (gmmastros) The SomeColumn column in the test table was created without specifying the collation, so it will inherit the default collation for that database. Instead of getting the collation for the column from information_schema, you could have used:


    SELECT * FROM Test t1
    join Test2 t2 ON t1.SomeColumn = t2.SomeColumn COLLATE Database_Default

    The same problem occurs when you create a temp table and/or table variable. If you don't specify the collation when building temp tables and table variables, they will use the default collation of TempDB. If this is different than the collation your columns, you will get the same error. When creating temp tables and table variables, it is best to specify the collation. I usually do this...

    Declare @Blah(SomeColumn VarChar(20) Collate Database_Default)

    Using Database_Default like this will actually cause the collation of the string column (built in TempDB) to match the default collation of the user database.
    10/26/09 @ 12:21
    Comment from: SQLDenis [Member] Email
    *****
    SQLDenis Thanks George


    >>The SomeColumn column in the test table was created without specifying the collation

    That is because you saw the CREATE TABLE script, this was just an example because if I tell you to look at 2 tables you would not know which table had the default collation without running a query, scripting out the table or looking in the designer...

    10/26/09 @ 12:27
    Comment from: George Mastros (gmmastros) [Member]
    George Mastros (gmmastros) True.

    Of course, you can always specify the collation on both sides, like this.

    SELECT * FROM Test t1
    join Test2 t2 ON t1.SomeColumn COLLATE Database_Default = t2.SomeColumn COLLATE Database_Default

    I don't recommend doing this, but it may get you out of a pinch.
    10/26/09 @ 12:34
    Comment from: SQLDenis [Member] Email
    SQLDenis George...that would double my scripts and waste my precioussss bandwith :-)
    10/26/09 @ 12:35

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)