Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the COLLATE in a JOIN in SQL Server?

Tags:

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.

like image 788
D.Roca Avatar asked Sep 02 '16 21:09

D.Roca


People also ask

How do you collate in SQL Server?

You can specify collations for each character string column using the COLLATE clause of the CREATE TABLE or ALTER TABLE statement. You can also specify a collation when you create a table using SQL Server Management Studio. If you do not specify a collation, the column is assigned the default collation of the database.

What does collate in SQL mean?

SQL Server collation refers to a set of character and character encoding rules, and influences how information is stored according to the order in the data page, how data is matched by comparing two columns, and how information is arranged in the T-SQL query statement.

What does collate Database_default mean?

MSDN states COLLATE DATABASE_DEFAULT clause casts the collation of an expression, column definition, or database definition to inherit the collation of the "current database". To complement MSDN, the "current database" is the context of the database where the query is executed.

Why we use collate in SQL?

Collations in SQL Server provide sorting rules, case, and accent sensitivity properties for your data. Collations that are used with character data types, such as char and varchar, dictate the code page and corresponding characters that can be represented for that data type.


2 Answers

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  
like image 188
Alex Kudryashev Avatar answered Sep 23 '22 16:09

Alex Kudryashev


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  
like image 22
Julio Nobre Avatar answered Sep 19 '22 16:09

Julio Nobre