I have a query with an except clause. Underneath the except, my 2nd query is coming from a different database than the one that the first query is using.
It looks something like this
SELECT field1 as a
FROM table 1
EXCEPT
USE differentdb
SELECT field2 as a
FROM table 2
I have also tried this
SELECT field1 as a
FROM table 1
EXCEPT
USE differentdb
SELECT field2 as a
FROM differentdb.dbo.table2
I realize that this is not allowed in SQL because I get the following error:
Msg 468, Level 16, State 9, Line 1 Cannot resolve the collation
conflict between "SQL_Latin1_General_CP1_CI_AS" and
"Latin1_General_CI_AS" in the equal to operation.
I am wondering if there is another way to write this query and accomplish a cross-db EXCEPT clause.
Change the collation of column on the fly like:
SELECT field1 COLLATE SQL_Latin1_General_CP1_CI_AS as a
FROM table 1
EXCEPT
SELECT differentdb.SchemaName.field2 as a
FROM table 2
Us can use collate clause to convert collation from other database.
SELECT field2 collate SQL_Latin1_General_CP1_CI_AS as a
FROM table 2
Here you should use correct collation - because it is not enough information what is collation of your first and second databases.
Or you can simply use
SELECT field2 collate database_default as a
FROM table 2
This will cause the collate clause to inherit the collation of the current database
Related
I have the following SQL script, trying to perform a union on two tables located on the same database. One table appears to have a different collation type
select * from ns.TurnsCOS
union all
select * from ns.TurnsValue
After running, I get the following error
I've read the other posts related to fixing collation issues, but adding 'Collate Latin1_General_Bin' after the from clause doesn't seem to work
How best can I resolve this collation issue between the two tables?
You have to change the collation of each column individually e.g.
select OtherCols, Col1, Col6, Col7
from ns.TurnsCOS
union all
select OtherCols, Col1 collate SQL_Latin1_General_CP1_CI_AS, Col6 collate SQL_Latin1_General_CP1_CI_AS, Col7 collate SQL_Latin1_General_CP1_CI_AS
from ns.TurnsValue
You can permanently change the column collation as follows - but you should be very cautious doing so as it may then conflict with other existing queries:
ALTER TABLE [TableName] ALTER COLUMN [Col_Name]
[Existing Data-Type] COLLATE [New_Collation] [NULL | NOT NULL];
GO
Ref: https://learn.microsoft.com/en-us/sql/relational-databases/collations/set-or-change-the-column-collation?view=sql-server-2017
A column in a SQLite db must be COLLATE NOCASE. I assume there is no way to add that capability to an existing table, so I'm prepare to recreate the table with it. How can I determine if the existing column is COLLATE NOCASE in order to avoid recreating the table every time it is opened?
How can I determine if the existing column is COLLATE NOCASE
The query
SELECT sql FROM sqlite_master WHERE type='table' AND tbl_name='my_table'
will give you the CREATE TABLE statement for that table. You could inspect the DDL to determine if the column is already defined as COLLATE NOCASE.
You might not need to do that at all if it is sufficient to change the collations in the query. I mean you can just overwrite it in the query. It won't affect constraints or index, but depending on your use case, it might be good enough.
To be clear: the collate clause in the table definition is just a default for the queries. You can overwrite this in the queries.
e.g.
WHERE column = 'term' COLLATE NOCASE
or
ORDER BY column COLLATE NOCASE
However, not that SQLite's LIKE doesn't honor collate clause (use pragma case_sensitive_like instead).
The easiest and most general way is store a version number somewhere (in another table, or with PRAGMA user_version).
If you want to check the column itself, use a query with a comparison that is affected by the column's collation:
SELECT Col = upper(Col)
FROM (SELECT Col
FROM MyTable
WHERE 0 -- don't actually return any row from MyTable
UNION ALL
SELECT 'x' -- lowercase; same collation as Col
);
I am with strange problem with Firebird 2.5.
My database has default charset = utf8.
I have a column p_nname in patienten table:
CREATE TABLE PATIENTEN (
P_NNAME VARCHAR(25) DEFAULT '' NOT NULL COLLATE UNICODE_CI,
I expect collation to work everywhere. I mean in WHERE and ORDER BY clauses.
What I have is working collation in WHERE. Two queries below give me similar result and it is good.
select * from patienten where p_nname='adler'
select * from patienten where p_nname='ADler'
Problem is ORDER BY clause does not work as I expect.
This SQL works as if the column has no UNICODE_CI collation.
select * from patienten order by p_nname
To get the needed result with good sorting I have to write so:
select * from patienten order by p_nname collate unicode_ci
Is there a way to omit COLLATE flag in ORDER BY clause?
Looks like a bug indeed, the documentation states:
The keyword COLLATE specifies the collation order for a string column
if you need a collation that is different from the normal one for this
column. The normal collation order will be either the default one for
the database character set or one that has been set explicitly in the
column's definition.
so it should work without specifing the collate clause in ORDER BY. I suggest you file a bug report.
When i issue a SQL query, sometimes I get the following error message:
Cannot resolve the collation conflict between "Latin1_General_CI_AS" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.
I often solve this problem just make both table collation same. so i need to know is there any quick way to fix this issue.
I'd like to put something special in my SQL query as a result if collation is not same for both table in sql query then also query will work perfectly without any error. is there any solution?
You can force which collation by using the COLLATE clause.
i.e.
SELECT *
FROM Table1 T1
INNER JOIN Server2.dbo.Table2 T2
ON T1.Name = T2.Name COLLATE database_default
Collation conflicts are common when joining tables between two databases or servers, especially if the version of the DB is different.
You can specify a collation in a query using the collate clause:
where col1 = col2 collate Latin1_General_CI_AS
There could be a possibility that both the servers use different Collations. If yes then you would get an error similar to then one I mentioned at the top of this topic. What you should do in this case?
You can alter the default Collation of either of the table columns/fields, but this could have a ripple effect for other tables currently in use with the altered table.
Use COLLATE DATABASE_DEFAULT keyword while matching the columns/fields
like:
SELECT T1.EmployeeName, T2.DeptName
FROM ServerA.dbo.EmpTab T1
JOIN ServerB.dbo.DeptTab T2
ON T1.DeptCode COLLATE DATABASE_DEFAULT
= T2.DeptCode COLLATE DATABASE_DEFAULT
I have the problem, that MSSQL Server 2000 should select some distinct values from a table (the specific column is of the nvarchar type).
There are the sometimes the same values, but with different cases, for example (pseudocode):
SELECT DISTINCT * FROM ("A", "a", "b", "B")
would return
A,b
But I do want (and do expect)
A,a,b,B
because they actually are different values.
How to solve this problem?
The collation will be set to case insensitive.
You need to do something like this
Select distinct col1 COLLATE sql_latin1_general_cp1_cs_as
From dbo.myTable
Not sure about MS SQL but with MySQL or postgres, use BINARY for this operation. Cast the column to binary like so:
SELECT DISTINCT BINARY(column1) from table1;
Just change column1 and table1 as per your schema.
Full example that works for me in MySQL 5.7, should work for others:
SELECT DISTINCT BINARY(gateway) from transactions;
Cheers!
SELECT DISTINCT
CasedTheColumn
FROM
(
SELECT TheColumn COLLATE LATIN1_GENERAL_BIN AS CasedTheColumn
FROM myTAble
)FOO
WHERE
CasedTheColumn IN ('A', 'a'...)
Try setting the collation of the column in question to something binary, e.g. utf8-bin. You can either do that in the SELECT statement itself or by changing your table structure directly (which means it doesn't have to map the collation each time the query is run, since it will store it correctly internally).