Skip to content

Commit dbf2cd3

Browse files
authored
Merge pull request #13127 from pmasl/patch-707
Update set-or-change-the-column-collation.md
2 parents e946d25 + f145b10 commit dbf2cd3

1 file changed

Lines changed: 52 additions & 18 deletions

File tree

‎docs/relational-databases/collations/set-or-change-the-column-collation.md‎

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,67 @@ monikerRange: "=azuresqldb-current||>=sql-server-2016||=sqlallproducts-allversio
1818
[!INCLUDE[appliesto-ss-asdb-xxxx-xxx-md](../../includes/appliesto-ss-asdb-xxxx-xxx-md.md)]
1919
You can override the database collation for **char**, **varchar**, **text**, **nchar**, **nvarchar**, and **ntext** data by specifying a different collation for a specific column of a table and using one of the following:
2020

21-
- The COLLATE clause of [CREATE TABLE](../../t-sql/statements/create-table-transact-sql.md) and [ALTER TABLE](../../t-sql/statements/alter-table-transact-sql.md). For example:
22-
23-
```sql
24-
CREATE TABLE dbo.MyTable (
25-
IdCol int PRIMARY KEY,
26-
CharCol VARCHAR(10) COLLATE French_CI_AS NOT NULL
27-
);
28-
GO
29-
ALTER TABLE dbo.MyTable
30-
ALTER COLUMN CharCol VARCHAR(10) COLLATE Latin1_General_CI_AS NOT NULL;
31-
GO
32-
```
33-
34-
- [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)]. For more information, [Collation and Unicode Support](../../relational-databases/collations/collation-and-unicode-support.md).
21+
- The COLLATE clause of [CREATE TABLE](../../t-sql/statements/create-table-transact-sql.md) and [ALTER TABLE](../../t-sql/statements/alter-table-transact-sql.md), as seen in the examples below.
22+
23+
- **In-place conversion.** Consider one of the existing tables defined below:
24+
25+
```sql
26+
-- NVARCHAR column is encoded in UTF-16 because a supplementary character enabled collation is used
27+
CREATE TABLE dbo.MyTable (CharCol NVARCHAR(50) COLLATE Latin1_General_100_CI_AI_SC);
28+
29+
-- VARCHAR column is encoded the Latin code page and therefore is not Unicode capable
30+
CREATE TABLE dbo.MyTable (CharCol VARCHAR(50) COLLATE Latin1_General_100_CI_AI);
31+
```
32+
33+
To convert the column in-place to use UTF-8, run an `ALTER COLUMN` statement that sets the required data type and a UTF-8 enabled collation:
34+
35+
```sql
36+
ALTER TABLE dbo.MyTable
37+
ALTER COLUMN CharCol VARCHAR(50) COLLATE Latin1_General_100_CI_AI_SC_UTF8
38+
```
39+
40+
This method is easy to implement, however it's a possibly blocking operation which may become an issue for large tables and busy applications.
41+
42+
- **Copy and replace.** Consider one of the existing tables defined below:
43+
44+
```sql
45+
-- NVARCHAR column is encoded in UTF-16 because a supplementary character enabled collation is used
46+
CREATE TABLE dbo.MyTable (CharCol NVARCHAR(50) COLLATE Latin1_General_100_CI_AI_SC);
47+
GO
48+
49+
-- VARCHAR column is encoded using the Latin code page and therefore is not Unicode capable
50+
CREATE TABLE dbo.MyTable (CharCol VARCHAR(50) COLLATE Latin1_General_100_CI_AI);
51+
GO
52+
```
53+
54+
To convert the column to use UTF-8, copy the data to a new table where the target column is already the required data type and a UTF-8 enabled collation, and then replace the old table:
55+
56+
```sql
57+
CREATE TABLE dbo.MyTableNew (CharCol VARCHAR(50) COLLATE Latin1_General_100_CI_AI_SC_UTF8);
58+
GO
59+
INSERT INTO dbo.MyTableNew
60+
SELECT * FROM dbo.MyTable;
61+
GO
62+
DROP TABLE dbo.MyTable;
63+
GO
64+
EXEC sp_rename 'dbo.MyTableNew', 'dbo.MyTable’;
65+
GO
66+
```
67+
68+
This method is much faster than in-place conversion, but handling complex schemas with many dependencies (FKs, PKs, Triggers, DFs) and synching the tail of the table (if the database is in use) requires more planning.
69+
70+
For more information, see [Collation and Unicode Support](../../relational-databases/collations/collation-and-unicode-support.md).
71+
72+
- [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)]. For more information, see [Modify Columns (Database Engine)](../../relational-databases/tables/modify-columns-database-engine.md#SSMSProcedure).
3573

3674
- Using the **Column.Collation** property in [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] Management Objects (SMO).
3775

3876
You cannot change the collation of a column that is currently referenced by any one of the following:
3977

4078
- A computed column
41-
4279
- An index
43-
4480
- Distribution statistics, either generated automatically or by the `CREATE STATISTICS` statement
45-
4681
- A CHECK constraint
47-
4882
- A FOREIGN KEY constraint
4983

5084
When you work with **tempdb**, the [COLLATE](~/t-sql/statements/collations.md) clause includes a *database_default* option to specify that a column in a temporary table uses the collation default of the current user database for the connection instead of the collation of **tempdb**.

0 commit comments

Comments
 (0)