How to set collation of a column with SQL? - sql

Originally, I created my SQL Server database on a local computer. I set its collation to Latin1_General_CI_AI and everything worked well. When I moved the finished work to the web hosting SQL Server, I encountered problem: they use a different database collation. So what can I do now?
To be more specific, I need Latin1_General_CI_AI, but they have Czech_CI_AS. These two differ significantly when comparing strings in Czech language (surprisingly I need latin1 general, not Czech, to get correct results.)
When I tried to change collation of my database, the server complained that I don't have user permission to do that. I tried to contact support desk, but no luck. Can I help myself?
I know that possibly each single table column can have its own collation, so maybe I should set up all my string columns to Latin1_CI_AI. But I don't know how to do this. I have only SQL access to the database (unfortunately no SQL Server Management Studio).

To change the database's collation
ALTER DATABASE MyDataBase COLLATE [NewCollation]
To change the collation of a column
ALTER TABLE MyTable ALTER COLUMN Column1 [TYPE] COLLATE [NewCollation]
But there are a number of limitations on when you can do this, very notably that this is denied if the column is used in any index.
You can do more in SSMS in some cases.
The syntax docs list the restrictions:
https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-table-transact-sql?view=sql-server-2017
https://learn.microsoft.com/en-us/sql/t-sql/statements/collations?view=sql-server-2017

I tried this code and it's working for me :
ALTER TABLE dbo.MyTable
ALTER COLUMN CharCol VARCHAR(50) COLLATE Latin1_General_100_CI_AI_SC_UTF8
reference : https://learn.microsoft.com/en-us/sql/relational-databases/collations/set-or-change-the-column-collation?view=sql-server-ver15

Related

How to make permanent change to the column collation?

Are there any configuration or option to make permanent change to the column collation so that I don't have to repeat COLLATE everywhere?
The following is my SQL query:
select
y.[Key] FieldName,
(select FieldName
from [dbo].GetFields(z.[Key])
where FieldType = y.[Key] COLLATE Chinese_Taiwan_Stroke_CI_AS) COLLATE Chinese_Taiwan_Stroke_CI_AS
from
FieldTableZ z
outer apply
FieldTableY y
It's unclear what are you trying to do here .. to change the collation of the entire server, the database or just for some columns? I'd try to answer all of them. A simple googling of "sql server change database collation" brings up these:
To change the server collation, there's the official documentation from Microsoft:
Set or Change the Server Collation
And if you want a step-by-step guide, here it is:
Changing SQL Server Collation After Installation - by Douglas Castilho
To change the database collation, for exmaple:
ALTER DATABASE CURRENT COLLATE SQL_Latin1_General_CP1_CI_AI
To change the collation of a table column:
Change SQL Server Database Collation - Paris Polyzos' blog
That is, you can alter the column collation with
ALTER TABLE [<Table>] ALTER COLUMN [<Column>] <ColumnType> COLLATE <NewCollation>
It's not the case if you are using graph tables; the graph internal columns cannot be altered that way.
I'd nevertheless guess that none of these are acceptable for what the query looks like -- you made your [Key] column (which looks like a primary key) duplicate in the query and trying to rely on the side effect of collation .. BTW, try not to let people just do the googling work for you.

Changing collation on schema

I have access to a database where the collation is set to SQL_Latin1_General_CP1_CI_AS. And of course I want it to be SQL_Latin1_General_CP1_CS_AS. Now the problem is just that there already exists tables in an other schema that I don't want to mess with. So first of, can I change the Collation of my own schema? And secondly, is that a ok way to do this? or is there any other, better way to deal with the situation?
Default colation cannot be specified per SQL Server database schema. The default collation can be specified per SQL Server database or per SQL Server instance only.
Setting default collation for SQL Server database does not change collation of existing database objects. It only specifies what collation is to be used by default when creating new database objects. So when you need to create multiple database objects with a specific collation you can beforehand change default database collation - it will not change the existing objects.
ALTER DATABASE MyDatabase COLLATE French_CI_AS ;
However changing default database collation becomes tricky when you have schema-bound objects that depend on the collation of the database. Then you recieve following error:
The object 'XY' is dependent on database collation. The database collation cannot be changed if a schema-bound object depends on it.
In that case it is better to specify collation for every column separately and explicitly like this:
CREATE TABLE dbo.MyTable
(PrimaryKey int PRIMARY KEY,
CharCol varchar(10) COLLATE French_CI_AS NOT NULL
);

Collation conflict ERROR ,Set collation for stored procedure as database default

How can I set for COLLATION for stored procedure as Database default rather than individual column of tables in that procedure .
Cannot resolve the collation conflict between “SQL_Latin1_General_Pref_CP1_CI_AS” and “Latin1_General_CI_AS” in the equal to operation
I am getting collation conflict error as database server collation is different from database collation. The procedure is huge and I don't know on which table column conflict is occuring.
Is it even possible to assign collation for stored procedure such that all columns in that procedure have same collation?
Firstly, collation is about how the textual data is stored within a table and is not at a stored procedure level.
Collation issues occur when textual data is compared from sources where the text collatation is not stored the same. (I am not talking about the data type of text, just string data)
Check the TSQL where clauses or table joins, this is the most common places where text compares occur. You can collate to the database default by using the collate function next to the compare area. For example
SELECT
c.CustomerID
FROM
dbo.Customer c
INNER JOIN dbo.CustomerLog cl on c.CustomerName = cl.LogEntry collate
database_default
Depending on your circumstance this might not be possible but I always follows these rules.
- Choose the correct server default collation at install so the tempdb is created under the collation, when you create temp tables, these are created in the tempdb
- when creating tables do not specifiy the collation unless you have specific cause to

Default collation of temporary tables

How can I check what the collation of a temporary table is?
I want to do this because I want to check what happens if I have database with a specific collation, different from the SQL Server instance and create a temporary table.
Let's say we have this scenario:
SQL Server 2008 - Latin1_General_CS_AS
Test Database - Estonian_CS_AS
Then create table #Test without specifying the collation. Which will be the collation of the table?
I think Estonian_CS_AS, but in the test I am doing is said Latin1_General_CS_AS. That's why I need to find a SQL statement to check this.
Note: from what I have read, I think that the collation of a temporary objects is defined by the tempdb collation. But if this is true, what defines its kind?
tempdb is recreated when the server start and gets the collation from the model database.
Temp tables you create without specifying the collation will have the collation of tempdb. When creating temp table you can use database_default as collation to get the same collation as the current database.

How to make a column case sensitive in sql 2005 or 2008

Is it possible to change the default collation based on a column? i want to make 1 column case sensitive but all the others not
ALTER TABLE ALTER COLUMN allows to change collation for a single column:
alter table Foo alter column Bar ntext collate Latin1_General_CS_AS
(collation might be incorrect)
I don't specifically know SQL Server, but the generally accepted DBMS practice (for compatibility) would be to either:
put insert and update triggers on the table so that they're stored in the case you want.
use generated columns to store another copy of the column in the case you want.
There may be a faster way to do it in SQL Server but you should be careful of solutions that push workload into the SELECT statements - they never scale well. It's almost always better doing this as part of inserts and updates since that's the only time data changes - doing it that way minimizes the extra workload.
The answer to your question is yes, already stated above by Anton Gogolev.
Additional Info:
Here is a how you can find list of Collation supported by your SQL Server based on its version.
select name,
COLLATIONPROPERTY(name, 'CodePage') as Code_Page,
description
from sys.fn_HelpCollations()
what is the meaning of Kanatype Sensitive KS and width sensitive