Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set collation of a column with SQL?

Tags:

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).

like image 328
Al Kepp Avatar asked Aug 30 '12 21:08

Al Kepp


People also ask

Can you set a collation for a column?

You cannot change the collation of a column that is currently referenced by any one of the following: A computed column. An index. Distribution statistics, either generated automatically or by the CREATE STATISTICS statement.

How do I change the collation setting in SQL?

You can change the collation of any new objects that are created in a user database by using the COLLATE clause of the ALTER DATABASE statement. This statement does not change the collation of the columns in any existing user-defined tables. These can be changed by using the COLLATE clause of ALTER TABLE.

What is column collation in SQL?

January 27, 2020 by Jignesh Raiyani. 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.

How do I collate data in SQL?

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.


1 Answers

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://docs.microsoft.com/en-us/sql/t-sql/statements/alter-table-transact-sql?view=sql-server-2017
  • https://docs.microsoft.com/en-us/sql/t-sql/statements/collations?view=sql-server-2017
like image 139
saul672 Avatar answered Sep 22 '22 09:09

saul672