SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

How to resolve string concatenation collation conflict?

Execute the following Microsoft SQL Server T-SQL example scripts in Management Studio Query Editor to create tables with different collation and construct queries with collation conflict and collate conflict resolution:

USE tempdb

GO

 

-- SQL select into create table

SELECT ContactID,

       FirstName,

       LastName

INTO   Contact_AS_Accent_Sensitive

FROM   AdventureWorks.Person.Contact

GO

-- (19972 row(s) affected)

 

-- Select into create table with different collation

SELECT ContactID,

       FirstName = FirstName COLLATE SQL_Latin1_General_CP1_CI_AI,

       LastName = LastName COLLATE SQL_Latin1_General_CP1_CI_AI

INTO   Contact_AI_Accent_Insensitive

FROM   AdventureWorks.Person.Contact

GO

-- (19972 row(s) affected)

 

-- String concatenation collation conflict in SELECT item concatenation

-- SQL Server string concatenation with the + operator

SELECT [Name] = s.FirstName + ' ' + i.LastName

FROM     Contact_AS_Accent_Sensitive s

         JOIN Contact_AI_Accent_Insensitive i

           ON s.ContactID = i.ContactID

 /* Msg 451, Level 16, State 1, Line 1

Cannot resolve collation conflict for column 1 in SELECT statement.

*/

        

-- String concatenation collation conflict in ORDER BY

SELECT [Name] = s.FirstName + ' ' + i.LastName

FROM     Contact_AS_Accent_Sensitive s

         JOIN Contact_AI_Accent_Insensitive i

           ON s.ContactID = i.ContactID

ORDER BY [Name]

GO

/* Msg 451, Level 16, State 1, Line 5

Cannot resolve collation conflict for column 1 in ORDER BY statement.

*/

 

 

-- Collation conflict resolved with COLLATE DATABASE_DEFAULT in SELECT

SELECT [Name] = s.FirstName + ' ' + i.LastName COLLATE DATABASE_DEFAULT

FROM     Contact_AS_Accent_Sensitive s

         JOIN Contact_AI_Accent_Insensitive i

           ON s.ContactID = i.ContactID

ORDER BY [Name]

GO

/* Partial results

 

Name

A. Leonetti

A. Scott Wright

A. Wright

Aaron Adams

*/

 

-- Collation conflict resolved with specific collation in SELECT

SELECT   [Name] = s.FirstName + ' ' + i.LastName COLLATE SQL_Latin1_General_CP1_CI_AI

FROM     Contact_AS_Accent_Sensitive s

         JOIN Contact_AI_Accent_Insensitive i

           ON s.ContactID = i.ContactID

ORDER BY [Name]

GO

 

/* Partial results

 

Name

A. Leonetti

A. Scott Wright

A. Wright

Aaron Adams

*/

-- Cleanup

DROP TABLE tempdb.dbo.Contact_AS_Accent_Sensitive

DROP TABLE tempdb.dbo.Contact_AI_Accent_Insensitive

------------

 

Related articles:

http://www.sqlusa.com/bestpractices2005/collatedatabasedefault/

Collation Precedence (Transact-SQL)

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.