Ignore Case Sensitivity While Querying Data - sql

I have a database in Azure SQL Server with collation as 'SQL_Latin1_General_CP1_CS_AS' and in that i have table that has two columns
CREATE TABLE DBO.TABLE1(
[ROWID] [numeric](16, 0) IDENTITY(1,1) NOT NULL,
[CODE] [varchar](10) NOT NULL,
)
Now below query works fine in Azure SQL ignoring the case sensitivity of column names
SELECT rowID, coDE FROM DBO.TABLE1
SELECT rowid, code FROM DBO.TABLE1
But when i create the database on a Sql Server inside Azure VM (IaaS solution) with same collation setting: 'SQL_Latin1_General_CP1_CS_AS' then above queries are not working at all its throwing below error:
Msg 207, Level 16, State 1, Line 1
Invalid column name 'rowid'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'code'.
How do i suppress this case sensitivity of column name while querying tables in Sql server on Azure VM.
Please advice.

Azure SQL Database always uses contained database collation, which allows you to have a case-sensitive database without having a case-sensitive catalog (yuck).
In a contained database, the catalog collation
Latin1_General_100_CI_AS_WS_KS_SC. This collation is the same for all
contained databases on all instances of SQL Server and cannot be
changed.
In SQL Sever on an Azure VM you can get the same behavior by using a contained database. Your database collation (and the default collation of all your table columns) can be SQL_Latin1_General_CP1_CS_AS, but your table names, column names, proc names, etc will use the case-insensitive Latin1_General_100_CI_AS_WS_KS_SC.

SQL_Latin1_General_CP1_CS_AS is case-sensitive, compare to SQL_Latin1_General_CP1_CI_AS which is case-insensitive. It's better to change your DB collation.

Related

Linked server reference "Invalid Column Name"

In SSMS 2012, I have created a linked server in SERVERA to SERVERB from which I have successfully written queries to multiple tables within the DBB database using a four part reference.
When I try to reference the 'Charge' table in the 'DBB' database with a simple select statement:
SELECT * FROM [SERVERB].[DBB].dbo.Charge
I get the following message:
Msg 207, Level 16, State 1, Line 1 Invalid column name 'charge_type'.
This column exists in the DBB database as 'Charge_Type', however, the collation of SERVERB is case insensitive, whereas the collation of SERVERA is case sensitive (which is where, I believe, my problem lies).
Does anyone have experience with this issue?
(For the people who might end up here)
You can change the collation on the fly. In this case, you have to write the name of the column names in the select query. What I mean is,
rather than writing query like this:
SELECT * FROM [SERVERB].[DBB].dbo.Charge
write the query like this:
SELECT Charge_Col1, Charge_Col2, Charge_Type COLLATE Latin1_General_CI_AS FROM [SERVERB].[DBB].dbo.Charge
There is another post similar to this: how we can select two columns having different collation

Collation conflict using 2 versions of SQL Server

I am working on 2 versions of SQL Server i.e 2005 and 2008 R2.
From 2008 R2 I have created a linked server which will connect to an older SQL Server 2005 instance.
I have one table on my server (2008) which is as below
members
id name
0002320 AOne Enterprises Motihari
0002321 AOne Enterprises Siliguri
Another table which resides on remote server contain activity of each agent
id member_code agent rr_no txn_date amount
I fired a query below
select top 5 *
from [192.168.6.3].sync.dbo.agents_log
where member_code IN
(select id from members where name like 'AOne Enterprises%')
I was trying to pull all activity log of AOne Enterprises through out the country which is in distributed database , so I need to create a link servers.
I got this error:
Msg 468, Level 16, State 9, Line 1
Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_BIN" in the equal to operation.
not exactly sure what you need, but if its only collation issue you can do below
SELECT TOP 5 *
FROM [192.168.6.3].sync.dbo.agents_log
WHERE member_code COLLATE SQL_Latin1_General_CP1_CI_AS
IN (SELECT id
FROM members
WHERE NAME LIKE 'AOne Enterprises%')
I just added COLLATE SQL_Latin1_General_CP1_CI_AS , perhaps it work

Using sys.dm_fts_index_keywords_by_document in SQL Server 2012

I'm trying to determine how many times a word occurs within a table for a uni assignment. I've been using sys.dm_fts_index_keywords_by_document in SQL Server 2012 as I've used it previously in 2008. It worked fine before, but in this context SQL Server doesn't seem to like it very much.
SELECT display_term, SUM(occurrence_count) AS APP
FROM sys.dm_fts_index_keywords_by_document
(
DB_ID('Assign2A_341'), OBJECT_ID('Post')
)
GROUP BY display_term
ORDER BY APP DESC
I keep running into this error: Msg 30004, Level 16, State 1, Line 1
A fulltext system view or stvf cannot open user table object id 599673184.
This is the format of the table being used:
CREATE TABLE Post(
Post_ID FLOAT NOT NULL,
Post_Txt NVARCHAR(MAX) NOT NULL,
Post_Date NVARCHAR(255) NOT NULL,
Post_Author VARCHAR(50) NOT NULL,
PRIMARY KEY(Post_ID));
I can't see any reason why this shouldn't work, the context in which I previously used it was very similar to how I'm using it now, the only difference being the version of SQL Server I'm using and the content of the table.
Any help would be very appreciated!
Did you create the full-text index on Table Post after the create Table statement?
Also, as BoL states, you need sufficient permissions to read from this index, are those present?
Requires CREATE FULLTEXT CATALOG permissions and SELECT permission on
the columns covered by the full-text index.

How can I convert "SQL_Latin1_ General" Collate Using an SSIS Package?

We've just noticed 2 of our servers have been set up using SQL_Latin1_General_CP1_CI_AS and Latin1_General_CI_AS.
I've created a script which compares two sources of data again each other (as it's a simple SELECT statement I haven't included it, it uses the same columns but from different sources) when I tried to union them together I received the collate error.
Msg 468, Level 16, State 9, Line 2
Cannot resolve the collation conflict between
"SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_CI_AS" in the UNION operation.
One of the sources is a table which is being populated by an SSIS package, is there a way of adapting the SSIS project to convert the "SQL_Latin1_General_CP1_CI_AS" format to "Latin1_General_CI_AS" before it's sent to the destination table?
Thanks!
You can use the sentence COLLATE Latin1_General_CI_AS after each column in the table that use SQL_Latin1_General_CP1_CI_AS like this.
SELECT COLUMN_A COLLATE Latin1_General_CI_AS AS COL1
FROM TABLE
Your error message looks like your UNION is in a single SQL statement?
e.g SELECT ... UNION ... SELECT
As Shiva described, within your SSIS Data Flow, you can use two (or more) OLE DB Source objects and combine them with a "Union All" transformation. Each OLE DB Source object will have just a single SELECT with no UNION clause. This design also has advantages in design/maintenance (unions on column names, no need to fill every column from every source) and runtime throughput.
I think you also need to use this technique on the OLE DB Source objects.
http://blog.stevienova.com/2009/04/16/ssis-pulling-data-from-a-non-default-collation-db-to-a-default-collation-db/

How I can obtain the collation of a specific table in a database?

How I can obtain the collation of a specific table in a database? Is it possible that a table have different collation in db?
Collation at the table level is on a per column basis, so it is possible to have a collation different than the database. If the collation is not defined at the column level, it defaults to the database collation setting.
SQL Server 2000:
SELECT c.name,
c.collation
FROM SYSCOLUMNS c
WHERE [id] = OBJECT_ID('your_table_name')
SQL Server 2005+:
SELECT c.name,
c.collation_name
FROM SYS.COLUMNS c
JOIN SYS.TABLES t ON t.object_id = c.object_id
WHERE t.name = 'your_table_name'
There is no such thing as a collation for a table.
A database has a default collation (which defaults to the collation for the server).
The default collation for the database will be applied to any column you add to a table, UNLESS you explicitly specify a collation at the column level.
Just as a point of note. SSMS can show column collations when scripting out the table. You can go into Tools-> Options. Under SQL Server Object Explorer -> Scripting, there is an option to show collation when scripting out a table. You can turn this on temporarily and script out a table to see table creation code including column collations.