SQL 将 Varchar 转换为 NVarchar?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7837898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting Varchar to NVarchar?
提问by Curt
I know when converting from nvarchar
to varchar
, some data will be lost/changed.
我知道从nvarchar
to转换时varchar
,某些数据会丢失/更改。
However, are there any risks of data changing when converting a varchar
data type to an nvarchar
?
但是,将varchar
数据类型转换为nvarchar
.
采纳答案by musefan
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.
nvarchar 存储的 unicode 字符是 varchar 字符大小的两倍。所以只要你的 nvarchar 至少是你的 vchar 长度的两倍,这就不成问题。
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)
如果您没有使用 ASCII 字符范围之外的任何字符(即您没有 Unicode 字符),则仍然可以实现另一种方式的转换
In short, make sure your nvarchar lengths are twice the size of your largest varchar value and then make the change.
简而言之,请确保您的 nvarchar 长度是最大 varchar 值的两倍,然后进行更改。
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
如果您正在谈论使用 SQL 更改长度,那么我认为您应该可以使用相同的长度。这是因为当您根据字符数而不是实际存储的数据量指定长度时
回答by TheBoyan
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.
大多数情况下,这两种数据类型在如何在 SQL Server 中或从应用程序中使用它们的方式上是相同的。不同之处在于 nvarchar 用于存储 unicode 数据,该数据用于在您的数据库表中存储多语言数据。其他语言有一组需要保存的扩展字符代码,此数据类型允许进行此扩展。如果您的数据库不会存储多语言数据,则应改用 varchar 数据类型。这样做的原因是 nvarchar 占用的空间是 varchar 的两倍,这是因为需要存储其他语言的扩展字符代码。
(from: http://weblogs.asp.net/guys/archive/2005/01/15/353550.aspx)
(来自: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.
因此 nvarchar 是 varchar 的超集,因此在转换时不应该丢失数据。
回答by Purplegoldfish
From this article http://searchsqlserver.techtarget.com/tip/Differences-between-varchar-and-nvarchar-in-SQL-Server
从这篇文章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.
VARCHAR 存储为常规 8 位数据。但是 NVARCHAR 字符串以 UTF-16 的形式存储在数据库中——每个字符 16 位或两个字节,始终——并转换为输出时数据库连接使用的任何代码页(通常是 UTF-8)。也就是说,NVARCHAR 字符串与它们的 VARCHAR 表亲具有相同的长度限制 - 8,000 字节。但是,由于 NVARCHAR 为每个字符使用两个字节,这意味着给定的 NVARCHAR 最多只能容纳 4,000 个字符(不是字节)。