SQL Server 2008 Collation conflict - how to resolve? - sql

For simplification, POC, I have the following query, using character typed columns:
select AH_NAME1 from GGIMAIN.SYSADM.BW_AUFTR_KOPF
union
select AH_NAME1 from GGI2014.SYSADM.BW_AUFTR_KOPF
and I get the following error:
Msg 468, Level 16, State 9, Line 2
Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_CS_AS" in the UNION operation.
GGI2014 was indeed created with collation SQL_Latin1_General_CP1_CI_AS. This has been changed in SMS and the instance has been restarted, also in SMS.
When I look in SMS, as well as query:
select name, collation_name from sys.databases
all indications are that both GGIMAIN and GGI2014 are collated Latin1_General_CS_AS.
Does anyone have any advice on what else needs to be done?
Thanks,
Matt

select AH_NAME1 COLLATE DATABASE_DEFAULT from GGIMAIN.SYSADM.BW_AUFTR_KOPF
union
select AH_NAME1 COLLATE DATABASE_DEFAULT from GGI2014.SYSADM.BW_AUFTR_KOPF
Unless I am mistaken, changing the collation of the database does not change the collation of the already existing objects. Only new objects will be affected

Try this one (maybe you're columns have different collation) -
SELECT AH_NAME1 COLLATE database_default
FROM GGIMAIN.SYSADM.BW_AUFTR_KOPF
UNION
SELECT AH_NAME1 COLLATE database_default
FROM GGI2014.SYSADM.BW_AUFTR_KOPF

I add a collate for each field of the query
SELECT Field1 collate default_database
,field2 collate default_database
,fieldn collate default_database
From DB1.dbo.table_x
UNION ALL
SELECT Field1 collate default_database
,field2 collate default_database
,fieldn collate default_database
From DB2.dbo.table_y
PD.only in the query that gives the error
I hope it works and I got them out of trouble

Related

Regular Collation issue but collate statement on join still giving same error

I have the following query:
Update
AEB
Set
[hbs_mailreturned] = 1
from
AccountExtensionBase AEB
Inner Join #IndividualsIDs I1 on
AEB.hbs_organisationid collate SQL_Latin1_General_CP1_CI_AS = I1.hbs_individualid collate SQL_Latin1_General_CP1_CI_AS
It is giving the following error message:
Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_CI_AI" in the equal to operation.
I don't understand this because I have specifying the collation to use on the join statement. What am I missing here?
I guess this will not create issue which you have applied on your columns,
AEB.hbs_organisationid collate SQL_Latin1_General_CP1_CI_AS = I1.hbs_individualid collate SQL_Latin1_General_CP1_CI_AS
please check for null for column values applied in join, if still not working then you can try
where fieldname COLLATE DATABASE_DEFAULT = secondfieldname COLLATE DATABASE_DEFAULT

SQL Server COLLATION ISSUE

I have table has field CHAR(3) collation set as database-default.
Database default is SQL_Latin1_General_CP1_CI_AS
I run a query where I am dumping the result of a stored proc into a temp table where I declare the field in question as CHAR(3)
CREATE TABLE #TempTable (
...
FieldAChar3 CHAR(3) NOT NULL
...
)
No mention of collation so I assume it uses the default!?
I do a union of my table and temp table on the field in question
SELECT Field1Char3 FROM Table1
UNION
SELECT FieldAChar3 FROM #TempTable
and I get a collation error of:
Msg 468, Level 16, State 9, Line 90
Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_CI_AS" in the UNION operation.
Obvious I can apply the collate statement of the default setting
collate database_default or
collate SQL_Latin1_General_CP1_CI_AS
to the second union statement as a work around but why am I getting the error in the first place?
Is it possible the collation was not applied like an SQL Server bug.
I can verify the collation was changed on the database prior to the creation of the table.
Your tempdb and database have different collation settings. This often happens when servers are upgraded from a version using the older collation defaults.
You could create the temp table with the SQL_Latin1_General_CP1_CI_AS collation.
There are few differences between SQL_Latin1_General_CP1_CI_AS and Latin1_General_CI_AS. They both use the same Locale (1033) and the same Code Page (1252).
The SQL_ Collations have been deprecated for some time and should be avoided if possible.

Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_CI_AS" in the equal to operations

This is my sql query
DELETE FROM [Prospects].[dbo].[P1]
WHERE RTRIM(LTRIM(DomainName)) IN
(SELECT RIGHT(UserEmail, LEN(UserEmail) - CHARINDEX('#', UserEmail)) UserEmail
FROM [Recruiter].[dbo].[tblSystemUser])
How to resolve this?
You can add
COLLATE DATABASE_DEFAULT
to the end of your query to convert it and fix the error, however this will take a performance hit.
As jarlh has suggested, changing the collation of the databases to be the same is the actual fix.
Try adding COLLATE DATABASE_DEFAULT to your query :
DELETE FROM [Prospects].[dbo].[P1]
WHERE RTRIM(LTRIM(DomainName)) COLLATE DATABASE_DEFAULT IN
(SELECT RIGHT(UserEmail, LEN(UserEmail) - CHARINDEX('#', UserEmail)) COLLATE DATABASE_DEFAULT UserEmail
FROM [Recruiter].[dbo].[tblSystemUser])
Try it's
DELETE FROM [Prospects].[dbo].[P1]
WHERE RTRIM(LTRIM(DomainName)) COLLATE DATABASE_DEFAULT IN
(SELECT RIGHT(UserEmail, LEN(UserEmail) - CHARINDEX('#', UserEmail)) UserEmail
FROM [Recruiter].[dbo].[tblSystemUser
])

case sensitive sql search in vb.net

So I've been using the query builder in visual studio (visual basic) to search a local mdb file. I have a button that is clicked to make a prompt for a search, and it works fine except that is is not case sensitive. Here is what I have so far:
SELECT ID, LastName, FirstName, FullTime, HireDate, Salary
FROM SalesStaff
WHERE LastName like ? + '%'
My professor wants us to use the InStr fuction, but how do I get that to work with a prompt?
(InputBox in my vb form code). Furthermore, it doesn't seem to have case sensitivity either. This is the first time I am using SQL, so I hardly know what I'm doing.
Thanks in advance!
You can amend the collation settings of your database/table.
Alternatively if you just want a case sensitive comparison on this one statement you can use the collate keyword, such as below:
select 1 where 'abc' = 'ABC'
select 1 where 'abc' collate Latin1_General_CS_AS = 'ABC' collate Latin1_General_CS_AS
select 1 where 'abc' collate Latin1_General_CI_AS = 'ABC' collate Latin1_General_CI_AS
select 1 where upper('abc') collate Latin1_General_CS_AS = 'ABC' collate Latin1_General_CS_AS
select 1 where upper('abc') collate Latin1_General_CI_AS = 'ABC' collate Latin1_General_CI_AS
CI stands for case insensitive.
CS stands for case sensitive.

SQL Server Collation conflict - creating a view

i am trying to create a View in a Database A, that is filled by a select from the Database B and i am having a collation conflict, to be more exactly , its between ( Latin1_General_CI_AS" and "Latin1_General_BIN ). WHere(in the code) i need to put the collate?
Best Regards.
The code is here:
CREATE VIEW [dbo].[CML_SDG_MENSAL_ESTOQUE]
AS
select
SUM(dw_fato_faturmes.val_fatur) val_fatur,
SUM(dw_fato_faturmes.val_receita) val_receita,
SUM(dw_fato_faturmes.qtd_bonif_item) qtd_bonif_item,
SUM(dw_fato_faturmes.val_bonif_fatur) val_bonif_fatur,
SUM(dw_fato_faturmes.val_bonif_receita) val_bonif_receita,
SUM(dw_fato_faturmes.val_devol_fatur) val_devol_fatur,
SUM(dw_fato_faturmes.val_devol_receita) val_devol_receita,
DW_DIM_PRODUTO.B1_CODDB B1_CODDB,
dw_fato_faturmes.cod_produto cod_produto,
SUM(dw_fato_faturmes.qtd_estoque) qtd_estoque,
SUM(dw_fato_faturmes.qtd_devol) qtd_devol,
SUM(dw_fato_faturmes.qtd_item) qtd_item,
SUM(dw_fato_faturmes.qtd_meta) qtd_meta,
SUM(dw_fato_faturmes.qtd_pedido) qtd_pedido,
SUM(dw_fato_faturmes.qtd_item)+
SUM(dw_fato_faturmes.qtd_bonif_item)+
SUM(dw_fato_faturmes.qtd_devol) venda_liquida
(SUM(dw_fato_faturmes.qtd_item)
+SUM(dw_fato_faturmes.qtd_bonif_item)
+SUM(dw_fato_faturmes.qtd_devol))
+SUM(dw_fato_faturmes.qtd_pedido) venda___pedido
FROM
logixbi.dbo.dw_fato_faturmes dw_fato_faturmes,
logixbi.dbo.DW_DIM_CLIENTE DW_DIM_CLIENTE,
DW_DIM_EMPRESA DW_DIM_EMPRESA,
logixbi.dbo.DW_DIM_MARCA DW_DIM_MARCA,
logixbi.dbo.DW_DIM_PRODUTO DW_DIM_PRODUTO,
logixbi.dbo.DW_DIM_REPRESENTANTE DW_DIM_REPRESENTANTE
where
DW_DIM_EMPRESA.SM0_FILIAL=dw_fato_faturmes.filial and
DW_DIM_MARCA.BM_GRUPO=dw_fato_faturmes.grupo and
DW_DIM_PRODUTO.B1_COD=dw_fato_faturmes.cod_produto and
DW_DIM_REPRESENTANTE.A3_COD=dw_fato_faturmes.vendedor and
DW_DIM_CLIENTE.A1_COD=dw_fato_faturmes.cliente and
DW_DIM_CLIENTE.A1_LOJA=dw_fato_faturmes.loja
group by DW_DIM_PRODUTO.B1_CODDB,dw_fato_faturmes.cod_produto
In order to find wich column has wich collation use this snippet:
SELECT name, collation_name
FROM sys.columns
WHERE OBJECT_ID IN (SELECT OBJECT_ID
FROM sys.objects
WHERE type = 'U'
AND name = 'your_table_name'
)
AND name = 'your_column_name'
Once you find the columns try this:
column_1 COLLATE your_collation = column_2 COLLATE your_collation
It is better to stick to a single collation globally. Otherwise you will have problems. Here is a snippet that will give you all the columns on your database with a COLLATION different than the one in the database
SELECT [TABLE_NAME] = OBJECT_NAME([id]),
[COLUMN_NAME] = [name],
[COLLATION_NAME] = collation
FROM syscolumns
WHERE collation <> 'your_database_collation_type'
AND collation IS NOT NULL
AND OBJECTPROPERTY([id], N'IsUserTable')=1
Where to put it depends on where the conflict is.
I'd suggest on the joins
ie
DW_DIM_EMPRESA.SM0_FILIAL COLLATE Latin1_General_CI_AS =dw_fato_faturmes.filial COLLATE Latin1_General_CI_AS
This is happening due to operation between different collation types so try this for statement for comparison.
ColumnA = ColumnB collate database_default
Try to use this in all your character matching conditions in where clause:
colnameA COLLATE Latin1_General_CI_AS = columnnameB COLLATE Latin1_General_CI_AS