将 Varchar 转换为 NVarchar?
我知道从 nvarchar
转换为 varchar
时,一些数据将会丢失/更改。
但是,将 varchar
数据类型转换为 nvarchar
时是否存在数据更改的风险?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
nvarchar 存储的 unicode 字符是 varchar 字符大小的两倍。因此,只要您的 nvarchar 长度至少是 vchar 长度的两倍,这就不成问题。
如果您没有使用 ASCII 字符范围之外的任何字符(即您没有 Unicode 字符),仍然可以实现另一种方式的转换。
简而言之,请确保您的 nvarchar 长度是最大 varchar 值的两倍,然后将改变。
由于我似乎对此收到了一些反对票(没有任何解释),因此我想澄清一下,当我提到上面的长度时,我指的是大小,例如所需的存储数据量。请注意我下面的评论,我也将在这里添加:
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:
编辑:上面的引用取自 http://weblogs.asp.net/ 的源页面不存在了。
有关详细信息,您可以尝试 MS 文档:
varchar:可变长度、非 Unicode 字符数据。数据库排序规则确定使用哪个代码页存储数据。
nvarchar:可变长度 Unicode 字符数据。取决于数据库排序规则进行比较。
nvarchar
是varchar
的“超集”,转换时不应该丢失数据。问题更多的是 ASCII 与 Unicode。EDIT: The above quote was taken from http://weblogs.asp.net/ from a source page that doesn't exist anymore.
For more info you can try MS docs:
varchar: Variable-length, non-Unicode character data. The database collation determines which code page the data is stored using.
nvarchar: Variable-length Unicode character data. Dependent on the database collation for comparisons.
nvarchar
is a "superset" ofvarchar
sort of speak, you shouldn't lose data when converting. The question is more ASCII vs Unicode.从本文http://searchsqlserver.techtarget。 com/tip/SQL-Server 中 varchar 和 nvarchar 之间的差异
From this article http://searchsqlserver.techtarget.com/tip/Differences-between-varchar-and-nvarchar-in-SQL-Server