I know when converting from nvarchar to varchar, some data will be lost/changed.

However, are there any risks of data changing when converting a varchar data type to an nvarchar?

有帮助吗?

解决方案

nvarchar stores unicode characters which are twice the size of varchar characters. So as long as your nvarchar is atleast twice the length of your vchar this will not be a problem.

Converting the other way could still be achieved providing you have not used any characters outside the ASCII character range (i.e. you do not have Unicode characters)

In short, make sure your nvarchar lengths are twice the size of your largest varchar value and then make the change.


As I seem to have received a couple of downvotes on this (with no explanation), I want to make it clear that when I refer to length above I mean the size, e.g. that amount of storage data required. Please note my comment below, which I will also include here:

If you are talking about changing the length with SQL then I think you should be ok to have the same length. This is because when you specify the length you do so based on the number of characters and not the actually amount of data that gets stored

其他提示

For the most part the two datatypes are identical in how you would work with them within SQL Server or from an application. The difference is that nvarchar is used to store unicode data, which is used to store multilingual data in your database tables. Other languages have an extended set of character codes that need to be saved and this datatype allows for this extension. If your database will not be storing multilingual data you should use the varchar datatype instead. The reason for this is that nvarchar takes twice as much space as varchar, this is because of the need to store the extended character codes for other languages.

(from: http://weblogs.asp.net/guys/archive/2005/01/15/353550.aspx)

So nvarchar is a superset of varchar therefore, you shouldn't lose data when converting.

From this article http://searchsqlserver.techtarget.com/tip/Differences-between-varchar-and-nvarchar-in-SQL-Server

VARCHAR is stored as regular 8-bit data. But NVARCHAR strings are stored in the database as UTF-16 — 16 bits or two bytes per character, all the time — and converted to whatever codepage is being used by the database connection on output (typically UTF-8). That said, NVARCHAR strings have the same length restrictions as their VARCHAR cousins — 8,000 bytes. However, since NVARCHARs use two bytes for each character, that means a given NVARCHAR can only hold 4,000 characters (not bytes) maximum.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top