How to use the COLLATE in a JOIN in SQL Server? - sql-server

I´m trying to join two tables but I get this error:
Msg 468, Level 16, State 9, Line 8 Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and
"Latin1_General_CI_AS" in the equal to operation.
This is the code I´m using:
SELECT *
FROM [FAEB].[dbo].[ExportaComisiones] AS f
JOIN [zCredifiel].[dbo].[optPerson] AS p
ON (p.vTreasuryId = f.RFC) COLLATE Latin1_General_CI_AS
I know it is wrong, it underlines COLLATE. I do not know how to apply it.

Correct syntax looks like this. See MSDN.
SELECT *
FROM [FAEB].[dbo].[ExportaComisiones] AS f
JOIN [zCredifiel].[dbo].[optPerson] AS p
ON p.vTreasuryId COLLATE Latin1_General_CI_AS = f.RFC COLLATE Latin1_General_CI_AS

As a general rule, you can use Database_Default collation so you don't need to figure out which one to use. However, I strongly suggest reading Simons Liew's excellent article Understanding the COLLATE DATABASE_DEFAULT clause in SQL Server
SELECT *
FROM [FAEB].[dbo].[ExportaComisiones] AS f
JOIN [zCredifiel].[dbo].[optPerson] AS p
ON (p.vTreasuryId = f.RFC) COLLATE Database_Default

Related

Why am I getting syntax error of bracket?

I have the following sql query:
\
(
Msg 102, Level 15, State 1, Line 16
Incorrect syntax near ')'.
Everything seems correct. but dont know why this error?
This isn't an answer. Rather it demonstrates what happens after considering all the comments. Here is your query after editing and formatting for readability. Warning - DISTINCT is often a kludge to cover up a logic or schema fault.
select distinct Baan1.t_pono, Baan1.t_oqua, Baan1.t_eono,
Baan1.t_odat, Baan2.t_bano, Baan2.t_orno, Baan2.t_send, Baan3.t_cuno
from BAAN1.baandb.dbo.ttdsls04020 Baan1
join [M3].[v850_Staging] Mthree on
Mthree.PONum1 collate DATABASE_DEFAULT = Baan1.t_eono collate DATABASE_DEFAULT
join BAAN1.baandb.dbo.ttcedi702200 Baan2
<see anything wrong here?>
join [M3].[v850_Staging] Mthree on
Mthree.PONum1 collate DATABASE_DEFAULT = Baan2.t_bano collate DATABASE_DEFAULT
join BAAN1.[baandb].[dbo].[ttcedi010200] Baan3 on
Mthree.Relation collate DATABASE_DEFAULT = Baan3.t_reno collate DATABASE_DEFAULT
<lack of ORDER BY clause is usually a fault>
<I added a statement terminator - which is a good habit you should develop
;
Notice that columns from Mthree are not used in the select list. Notice also that alias MThree is defined TWICE. It is odd that a "staging" table (view?) in one database is used to relate rows between tables in another database. Seems much more work is needed that goes beyond basic syntax issues.

Cannot resolve collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Arabic_100_CI_AS_KS_WS" in UNION ALL operator [duplicate]

This question already has an answer here:
Cannot Resolve Collation Conflict
(1 answer)
Closed 5 years ago.
i have to databases one of these database is in my server and another one is in another server that i have just read access to it just can select data from tables.
now i have made 2 views, first view is from my data base and it's name is "qryTransmittals_Lines_with_CT_CS"
and second view gets it,s data from other server and other database is (qryTqLines)
now i want to union all data from both queries but i got error:
Cannot resolve collation conflict between "SQL_Latin1_General_CP1_CI_AS"
and "Arabic_100_CI_AS_KS_WS" in UNION ALL operator occurring in SELECT
statement column 8.
SELECT qryTqLines.*
FROM qryTqLines
UNION ALL
SELECT qryTransmittals_Lines_with_CT_CS.*
FROM qryTransmittals_Lines_with_CT_CS;
i checked collation of my table in my server is (SQL_Latin1_General_CP1_CI_AS) and about another table i do not have access to modify design of table.
please help how i can solve my problem.
As i was provided link and OP solved his problem, so posting some code from provided link for future readers
Have to make both column with same Collate so we can compare.
1a. here we change collate of search.cat_id to collate SQL_Latin1_General_CP1_CI_AS from Latin1_General_CI_AS
SELECT search.*
FROM categories INNER JOIN search
ON categories.cid = search.cat_id collate SQL_Latin1_General_CP1_CI_AS
OR
1b. here we change collate of categories.cid to Latin1_General_CI_AS from collate SQL_Latin1_General_CP1_CI_AS
SELECT search.*
FROM categories INNER JOIN search
ON categories.cid collate Latin1_General_CI_AS = search.cat_id
reference link
http://www.ashishblog.com/how-to-resolve-the-collation-conflict-and-how-to-check-collate-in-sql-server/

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.

Collation conflict with temp table

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')

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

Resources