Published on

December 7, 2008

Changing Collation of a Database in SQL Server

Collation in SQL Server refers to the rules that determine how string comparison and sorting operations are performed. It defines the character set, case sensitivity, and accent sensitivity for a database or table column.

In some cases, you may need to change the collation of a database to match the requirements of your application or to resolve collation conflicts. Fortunately, changing the collation of a database in SQL Server is a straightforward process.

Let’s take a look at an example:

USE AdventureWorks

/* Create Test Table */
CREATE TABLE TestTable (
  FirstCol VARCHAR(10)
)

/* Check Database Column Collation */
SELECT name, collation_name
FROM sys.columns
WHERE OBJECT_ID IN (
  SELECT OBJECT_ID
  FROM sys.objects
  WHERE type = 'U' AND name = 'TestTable'
)

/* Change the database collation */
ALTER TABLE TestTable
ALTER COLUMN FirstCol VARCHAR(10) COLLATE SQL_Latin1_General_CP1_CS_AS NULL

/* Check Database Column Collation */
SELECT name, collation_name
FROM sys.columns
WHERE OBJECT_ID IN (
  SELECT OBJECT_ID
  FROM sys.objects
  WHERE type = 'U' AND name = 'TestTable'
)

/* Database Cleanup */
DROP TABLE TestTable

When you run the above script, it will give you two result sets. The first result set shows the default collation of the database before changing the column’s collation. The second result set shows the newly defined collation after changing the column’s collation.

Changing the collation of a database can be useful in scenarios where you need to compare or sort strings in a specific way. It can also help resolve collation conflicts when performing operations that involve multiple databases or tables with different collations.

If you encounter a “Cannot resolve collation conflict” error when performing an equal-to operation, you can refer to the solution provided in the reference section of this article.

Collation is an important aspect of SQL Server that can impact the behavior and performance of your queries. Understanding how to change the collation of a database gives you more control over how string operations are handled in your application.

Feel free to share your thoughts and any challenges you have faced with collation. I am interested in hearing from the SQL community and sharing solutions to common collation-related issues.

Reference: Pinal Dave (http://www.SQLAuthority.com)

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.