Collation conflict with temp table - sql-server

The collation of tempdb is Latin1_General_100_CI_AI. The collation of the database is also Latin1_General_100_CI_AI. Yet the following SQL statement:
SELECT *
FROM ##CitiesMapping AS cm
INNER JOIN Cities ON cm.CityName=Cities.Name
returns:
Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_100_CI_AI" in the equal to operation.
The server default collation is also Latin1_General_100_CI_AI

It is possible that the collation is set differently for a single column. The query from Stuart will show you that. If they are different collations you can specify the collation being used on either side of the comparison like this:
SELECT *
FROM ##CitiesMapping AS cm
INNER JOIN Cities
ON cm.CityName COLLATE DATABASE_DEFAULT = Cities.Name COLLATE DATABASE_DEFAULT;
I hope this helps you out.

Check the tables involved as well:
SELECT name, collation_name, OBJECT_NAME(object_id)
FROM sys.columns
WHERE OBJECT_NAME(object_id) IN ('Cities')

Related

Cannot resolve the collation conflict between "Latin1_General_CI_AI" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation

I am trying to create a procedure in SQL Server 2008R2 but it is showing this error
Cannot resolve the collation conflict between "Latin1_General_CI_AI"
and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.
The procedure that I have created is
CREATE Procedure Ps_Quiz_OnlineTest_QuestionsWithOptions_Get --'Ques1'
#Ques Varchar(8000)
As
Begin
Select
A.QuestionId,
A.QsnDesc,
A.CorrectOption,
B.OptionValue,
A.Marks,
(
Select QsnName
From Quiz_tblQsnsLimitMaster
Where QsnId = #Ques) QuesPaper,
(
Select Durationoftest
From Quiz_tblQsnsLimitMaster
Where QsnId = #Ques) QuesPaper
From
Quiz_tblQsnCreationMaster A,
Quiz_tblQsnCreationDetail B
Where
A.QuestionId = B.QuestionId
And A.QuestionId In (
Select QuestionIds
From FN_Question_Answers_Quiz(#Ques))
And B.QuestionId In (
Select QuestionIds
From FN_Question_Answers_Quiz(#Ques))
Order By
A.QuestionId,
B.OptionOrder
End
I tried to collate tables with different collations but it did not worked.
How can I solve this.
probably this might help, change your where condition like this
Where A.QuestionId COLLATE DATABASE_DEFAULT = B.QuestionId COLLATE DATABASE_DEFAULT
Since you've mentioned that QuestionID in each table is varchar, it's probably the comparison between those. So at a guess, try changing the line:
Where A.QuestionId = B.QuestionId And
To the following:
Where A.QuestionId = B.QuestionId COLLATE SQL_Latin1_General_CP1_CI_AS And
I'd prefer it if you used proper join syntax for your tables - but that's for another day ;-)
try use this,
where fieldname COLLATE DATABASE_DEFAULT = secondfieldname COLLATE DATABASE_DEFAULT
this is working
Just use the following syntax when joining tables with different collations,
where A.QuestionId collate SQL_Latin1_General_CP1_CI__AS = B.QuestionId collate
SQL_Latin1_General1_General_CP1_CI_AS
To overcome this use the below code:
SELECT NAME
FROM sys.objects
WHERE NAME COLLATE DATABASE_DEFAULT NOT IN (SELECT TYPE
FROM sys.objects)
I was facing same issue after migration of Database and shifting of server by service provider. So as solution I have created new database using CPanel then execute SQL query for schema and data to restore. Finally this issue got resolved.

SQL-Server is ignoring my COLLATION when I'm using LIKE operator

I'm working with Spanish database so when I'm looking for and "aeiou" I can also get "áéíóú" or "AEIOU" or "ÁÉÍÓÚ", in a where clause like this:
SELECT * FROM myTable WHERE stringData like '%perez%'
I'm expencting:
* perez
* PEREZ
* Pérez
* PÉREZ
So I changed my database to collation: Modern_Spanish_CI_AI
And I get only:
* perez
* PEREZ
But if I do:
SELECT * FROM myTable WHERE stringData like '%perez%' COLLATE Modern_Spanish_CI_AI
I get all results OK, so my question is, why if my database is COLLATE Modern_Spanish_CI_AI I have to set the same collation to my query???
I'm using SQL-Server 2008
You can use COLLATE, eg.
SELECT *
FROM TableName
WHERE strData COLLATE Latin1_general_CI_AI = 'perez' COLLATE Latin1_general_CI_AI
both sides must have the same collation.
SQLFiddle Demo
SQLFiddle Demo (using LIKE)
Others:
Selecting a SQL Server Collation
You need to change the collation of the table COLUMN itself.
select collation_name, *
from sys.columns
where object_id = object_id('tblname')
and name = 'stringdata';
If you're lucky it is as easy as (example)
alter table tblname alter column stringdata varchar(20) collate Modern_Spanish_CI_AS
But if you have constraints and/or schema bound references, it can get complicated.
It can be very difficult to work with a database with mixed collations, so you may want to re-collate all the table columns.

Sql Server - Using Collation

I am using below query in which I need to specify collation hint to avoid collation issues across databases as this query uses tables from 2 databases.
Msg 468, Level 16, State 9, Line 12 Cannot resolve the collation
conflict between "Latin1_General_CS_AI" and "Latin1_General_CS_AS" in
the equal to operation.
Currently I am getting above error for collation conflicts when I run some of the queries which uses different databases with different collations:
Delete from table1 where oldcolumn in
(
select newcolumn from Database2.dbo.table2
where invoiceid = #invno
and complete = 0
)
I changed the query to include collation hint as below:
Delete from table1 where oldcolumn COLLATE SQL_Latin1_General_CP1_CS_AS in
(
select newcolumn from Database2.dbo.table2
where invoiceid = #invno
and complete = 0
)
Will above query solve the problem of collation?
Is it same to specify collate hint on left or right of operator (e.g. "=" operator)?
Can query like invoiceid = #invno ever generate runtime collation conflit error?
Note: I am asking this question as I do not have access to any of the above 2 databases and the script will be run on actual databases.
Use Below Query :
DELETE FROM Table1
WHERE Table1.ID IN (
SELECT Table1.ID
FROM Table1
INNER JOIN Database2.dbo.Table2 Table2 ON Table2.NewColumn = Table1.OldColumn COLLATE SQL_Latin1_General_CP1_CS_AS
WHERE Table2.invoiceid = #invno
AND Table2.complete = 0
)

SQL change field Collation in a select

i'm trying to do the following select:
select * from urlpath where substring(urlpathpath, 3, len(urlpathpath))
not in (select accessuserpassword from accessuser where accessuserparentid = 257)
I get the error:
Cannot resolve the collation conflict between
"SQL_Latin1_General_CP1_CI_AI" and
"SQL_Latin1_General_CP1_CI_AS" in the equal to operation.
Does anyone know how i can cast as a collation, or something that permits me to match this condition?
Thanx
You can add COLLATE CollationName after the column name for the column you want to "re-collate". (Note: the collation name is literal, not quoted)
You can even do the collate on the query to create a new table with that query, for example:
SELECT
*
INTO
#TempTable
FROM
View_total
WHERE
YEAR(ValidFrom) <= 2007
AND YEAR(ValidTo)>= 2007
AND Id_Product = '001'
AND ProductLine COLLATE DATABASE_DEFAULT IN (SELECT Product FROM #TempAUX)
COLLATE DATABASE_DEFAULT causes the COLLATE clause inherits the collation of the current database, eliminating the difference between the two

Collation conflict SQL Server 2008

I've been going around this but I haven't found a solution for my problem. My sql query is:
SELECT
dbo.Country.CtyRecID, dbo.Country.CtyShort, dbo.Notification.NotRecID,
dbo.Notification.NotName, dbo.TemporalSuspension.TCtsCode,
dbo.TemporalSuspension.TCtsCodeRecID,
dbo.TaxPhylum.PhyName AS Taxon, dbo.TemporalSuspension.TCtsNotes,
dbo.TemporalSuspension.TCtsRecID,
dbo.TemporalSuspension.TCtsKgmRecID,
CASE dbo.TemporalSuspension.TCtsKgmRecID WHEN 1 THEN 'Animals'
WHEN 2 THEN 'Plants' ELSE 'All' END AS Kingdom
FROM
dbo.TemporalSuspension
INNER JOIN dbo.Notification
ON dbo.TemporalSuspension.TCtsStartNotRecID = dbo.Notification.NotRecID
INNER JOIN dbo.Country
ON dbo.TemporalSuspension.TCtsCtyRecID = dbo.Country.CtyRecID
INNER JOIN dbo.TaxPhylum
ON dbo.TemporalSuspension.TCtsCodeRecID = dbo.TaxPhylum.PhyRecID
AND dbo.TemporalSuspension.TCtsCode LIKE 'PHY'
UNION ALL
SELECT
dbo.Country.CtyRecID, dbo.Country.CtyShort, dbo.Notification.NotRecID,
dbo.Notification.NotName, dbo.TemporalSuspension.TCtsCode,
dbo.TemporalSuspension.TCtsCodeRecID,
dbo.TaxClass.ClaName AS Taxon, dbo.TemporalSuspension.TCtsNotes,
dbo.TemporalSuspension.TCtsRecID,
dbo.TemporalSuspension.TCtsKgmRecID,
CASE dbo.TemporalSuspension.TCtsKgmRecID WHEN 1 THEN 'Animals'
WHEN 2 THEN 'Plants' ELSE 'All' END AS Kingdom
FROM
dbo.TemporalSuspension
INNER JOIN dbo.Notification
ON dbo.TemporalSuspension.TCtsStartNotRecID = dbo.Notification.NotRecID
INNER JOIN dbo.Country
ON dbo.TemporalSuspension.TCtsCtyRecID = dbo.Country.CtyRecID
INNER JOIN dbo.TaxClass
ON dbo.TemporalSuspension.TCtsCodeRecID = dbo.TaxClass.ClaRecID
AND dbo.TemporalSuspension.TCtsCode LIKE 'CLA'
But I don't understand why it doesn't work, I keep getting this error :
Cannot resolve collation conflict for column 7 in SELECT statement.
What's wrong? I've used this other times and I never got this problem. According to the error the dbo.TaxPhylum.PhyName AS Taxon, and dbo.TaxClass.ClaName AS Taxon, is the thing giving the problem, but I don't really understand why, both columns have the same type and everything.
EDIT: This is the result obtained with the query, how do I get around this?
Column Name Table Name collation_name
PhyName vDecisionsExpanded Latin1_General_CI_AS
ClaName vDecisionsExpanded SQL_Latin1_General_CP1_CI_AS
thanks
Try this query in your database:
SELECT
col.name 'Column Name',
OBJECT_NAME(object_id) 'Table Name',
col.collation_name
FROM sys.columns col
WHERE col.system_type_id IN (35, 99, 167, 175, 231, 239) -- TEXT, NTEXT, VARCHAR etc.
It will show you all string-related columns in your database, and their collation.
The error message says that column 7 is the culprit - that would be dbo.TaxPhylum.PhyName - so also check the TaxPhylum database. Is the collation in that database different from your normal database??
UPDATE:
if you have a collation conflict, you can do two things:
1) if it's only a single or a few columns in a SELECT, just add the COLLATE ..... modifier to them:
SELECT
.....
dbo.TaxPhylum.PhyName COLLATE SQL_Latin1_General_CP1_CI_AS AS Taxon,
.....
2) if it's a lot of columns, you might want to consider to modify the COLLATION on those columns / tables or in that database all together
How to change database or server collation
Different server/database combination? At the risk of asking the obvious, I assume that you have verified your columns collation sequences?

Resources