How to Change Column Collation in SQL Server A Step-by-Step GuideCollation in SQL Server defines how string comparison is handled, including rules for case sensitivity, accent sensitivity, and character set. Sometimes, developers or database administrators need to change the collation of a column to ensure consistency across the database or to fix compatibility issues when joining tables.
This topic will walk you through how to change column collation in SQL Server using simple language, useful examples, and step-by-step instructions. Whether you’re new to SQL Server or have some experience, this guide will help you handle collation changes confidently.
What Is Collation in SQL Server?
Collation determines how text is sorted and compared in SQL Server. It affects
-
Case sensitivity (
CASE_SENSITIVEvs.CASE_INSENSITIVE) -
Accent sensitivity (e.g., whether
éequalse) -
Language rules (e.g., alphabetical order)
Each column in a table can have its own collation, and this can lead to issues when querying or joining data from columns with different collations.
Why Change Column Collation?
There are several reasons why you may want to change the collation of a column
-
To match collation across databases or tables for compatibility.
-
To enforce case sensitivity or insensitivity.
-
To change sorting or comparison rules for specific languages.
-
To fix collation conflict errors in queries, especially during joins or unions.
How to Check the Current Collation
Before making any changes, you should know the current collation of your column. You can use the following SQL command
SELECT COLUMN_NAME, COLLATION_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'YourTableName';
This shows the collation used by each column in a given table.
General Syntax to Change Column Collation
To change the collation of a column, you use the ALTER TABLE statement combined with ALTER COLUMN, followed by the new collation setting.
ALTER TABLE TableNameALTER COLUMN ColumnName DataType COLLATE NewCollationName;
Make sure to specify the correct data type for the column. Collation can only be set on character-based data types like VARCHAR, CHAR, TEXT, NVARCHAR, etc.
Step-by-Step Example
Let’s say you have a column named LastName in the Employees table. Its current collation is different from what you need.
Step 1 Identify the Current Collation
SELECT COLUMN_NAME, COLLATION_NAMEFROM INFORMATION_SCHEMA.COLUMNSWHERE TABLE_NAME = 'Employees' AND COLUMN_NAME = 'LastName';
Step 2 Change the Collation
Suppose you want to change the collation to SQL_Latin1_General_CP1_CI_AS (case-insensitive, accent-sensitive)
ALTER TABLE EmployeesALTER COLUMN LastName VARCHAR(100) COLLATE SQL_Latin1_General_CP1_CI_AS;
If the column contains non-null values or is used in constraints, you’ll need to temporarily remove those constraints first.
Handling NULL and NOT NULL Columns
If the column is defined as NOT NULL, the syntax must include NOT NULL
ALTER TABLE EmployeesALTER COLUMN LastName VARCHAR(100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL;
If you omit NOT NULL in such a case, SQL Server will throw an error.
What About Columns in Primary or Foreign Keys?
If the column you want to alter is part of a primary key, foreign key, or index, you’ll need to
-
Drop the constraint or index temporarily.
-
Change the column collation.
-
Recreate the constraint or index.
Example
-- Drop index or constraint firstALTER TABLE Employees DROP CONSTRAINT PK_Employees;-- Change collationALTER TABLE EmployeesALTER COLUMN LastName VARCHAR(100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL;-- Recreate primary keyALTER TABLE EmployeesADD CONSTRAINT PK_Employees PRIMARY KEY (LastName);
Always make sure you understand the dependencies before dropping any constraints.
Changing Collation Across Multiple Columns
Sometimes you might need to update several columns in different tables. This can be done by generating a script from the system views
SELECT 'ALTER TABLE ' + TABLE_NAME + ' ALTER COLUMN ' + COLUMN_NAME + ' ' + DATA_TYPE + CASE WHEN CHARACTER_MAXIMUM_LENGTH = -1 THEN '(MAX)' ELSE '(' + CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR) + ')' END + ' COLLATE SQL_Latin1_General_CP1_CI_AS' +CASE WHEN IS_NULLABLE = 'NO' THEN ' NOT NULL;' ELSE ';' ENDFROM INFORMATION_SCHEMA.COLUMNSWHERE DATA_TYPE IN ('varchar', 'char', 'text', 'nvarchar', 'nchar', 'ntext');
This script creates multiple ALTER TABLE statements for columns with character data types.
What to Watch Out For
1. Data Loss
Changing collation does not delete data, but it’s good practice to back up your database first, especially for production environments.
2. Collation Conflict Errors
If you’re facing errors like
Cannot resolve collation conflict for equal to operation.
It usually means you’re comparing two columns with different collations. Changing one of them, or using COLLATE in the query, solves this.
Example
SELECT *FROM Table1 t1JOIN Table2 t2 ON t1.Name COLLATE SQL_Latin1_General_CP1_CI_AS = t2.Name COLLATE SQL_Latin1_General_CP1_CI_AS;
Best Practices
-
Keep your database and columns using the same default collation where possible.
-
Use Unicode data types (
NVARCHAR,NCHAR) when dealing with multilingual data. -
When designing databases, decide early on whether your system needs to be case-sensitive.
-
Test your changes in a development environment before applying them to production.
Changing the collation of a column in SQL Server is not complicated, but it requires careful planning, especially when constraints or data integrity are involved. By understanding the syntax and implications, you can manage collation changes confidently and avoid common pitfalls.
Whether you’re aligning collation across databases, adjusting case sensitivity, or fixing query errors, these steps will guide you through a successful update process. Always test, back up, and proceed with clarity.