Change datatype varchar to nvarchar in existing SQL Server 2005 database. Any issues?
Asked Answered
C

6

43

I need to change column datatypes in a database table from varchar to nvarchar in order to support Chinese characters (currently, the varchar fields that have these characters are only showing question marks).

I know how to change the values, but I want to see if it's safe to do so. Is there anything to look out for before I do the changing? Thanks!

Cioban answered 16/11, 2011 at 19:39 Comment(0)
B
55

Note that this change is a size-of-data update, see SQL Server table columns under the hood. The change will add a new NVARCHAR column, it will update each row copying the data from the old VARCHAR to the new NVARCHAR column, and then it will mark the old VARCHAR column as dropped. IF the table is large, this will generate a large log, so be prepared for it. After the update, run DBCC CLEANTABLE to reclaim the space used by the former VARCHAR column. If you can afford it , better run ALTER TABLE ... REBUILD, which will not only reclaim the space it will also completely remove physical deleted VARCHAR column. The linked article at the beginning has more details.

You may also be interested in enabling Unicode Compression for your table.

Bridgeboard answered 16/11, 2011 at 20:6 Comment(0)
F
50

You can do on non primary key fields:

ALTER TABLE [TableName]
ALTER COLUMN [ColumnName] nvarchar(N) null

On the primary key fields it will not work - you will have to recreate the table

Fogle answered 28/3, 2013 at 15:23 Comment(1)
Or drop then recreate the primary key, and any foreign keys that refer to itRefrigeration
V
10

Make sure that the length doesn't exceed 4000 since the maximum for VARCHAR is 8000 while NVARCHAR is only 4K.

Vezza answered 16/11, 2011 at 19:41 Comment(1)
By default, nvarchar(MAX) values are stored exactly the same as nvarchar(4000) values would be, unless the actual length exceed 4000 characters; in that case, the in-row data is replaced by a pointer to one or more seperate pages where the data is stored. NVARCHAR(MAX) may hold up 2GB of dataSweeping
S
5

The table will get bigger. Each character in the column will take twice the space to store. You might not notice unless the table is really big.

Stored procedures/views/queries that work with the column data might need to be modified to deal with the nvarchar.

Substitute answered 16/11, 2011 at 19:45 Comment(0)
A
5

Check all the dependencies for this table as stored procs, functions, temp tables based on this table and variables used for inserts/updates etc may also need to be updated to NVARCHAR. Also check if the table is in replication! That could cause you a new set of problems!

Ayakoayala answered 9/3, 2017 at 10:55 Comment(0)
D
2

Using Remus' answer for reference, here's a complete script that does the ALTER COLUMN and then reclaims the space. Note that on a mid-spec laptop with 16 GBs of RAM, this took less than 5 minutes to run on a table with > 5 million records. Less than 1 minute on my desktop workstation and the production server.

ALTER TABLE TableName ALTER COLUMN ColumnName nvarchar(250) not null

DBCC CLEANTABLE (DatabaseName, 'TableName', 100000)

ALTER TABLE TableName REBUILD
Dumbwaiter answered 2/3, 2023 at 23:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.