将 Varchar 转换为 NVarchar?

发布于 2024-12-11 04:34:35 字数 151 浏览 0 评论 0原文

我知道从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云之铃。 2024-12-18 04:34:35

nvarchar 存储的 unicode 字符是 varchar 字符大小的两倍。因此,只要您的 nvarchar 长度至少是 vchar 长度的两倍,这就不成问题。

如果您没有使用 ASCII 字符范围之外的任何字符(即您没有 Unicode 字符),仍然可以实现另一种方式的转换。

简而言之,请确保您的 nvarchar 长度是最大 varchar 值的两倍,然后将改变。


由于我似乎对此收到了一些反对票(没有任何解释),因此我想澄清一下,当我提到上面的长度时,我指的是大小,例如所需的存储数据量。请注意我下面的评论,我也将在这里添加:

如果您正在谈论使用 SQL 更改长度,那么我认为您
长度相同应该没问题。这是因为当您指定
您这样做的长度基于字符数而不是
实际存储的数据量

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

半仙 2024-12-18 04:34:35

在大多数情况下,这两种数据类型在您的方式上是相同的
在 SQL Server 中或从应用程序中使用它们。这
区别在于nvarchar用于存储Unicode数据,即
用于在数据库表中存储多语言数据。其他
语言有一组扩展的字符代码,需要
已保存并且此数据类型允许此扩展。如果你的数据库
不会存储多语言数据,您可以使用 varchar
数据类型代替。其原因是 nvarchar 需要两倍的时间
varchar 一样多的空间,这是因为需要存储
其他语言的扩展字符代码。

编辑:上面的引用取自 http://weblogs.asp.net/ 的源页面不存在了。

有关详细信息,您可以尝试 MS 文档:

varchar:可变长度、非 Unicode 字符数据。数据库排序规则确定使用哪个代码页存储数据。

nvarchar:可变长度 Unicode 字符数据。取决于数据库排序规则进行比较。

nvarcharvarchar 的“超集”,转换时不应该丢失数据。问题更多的是 ASCII 与 Unicode。

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 could 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.

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" of varchar sort of speak, you shouldn't lose data when converting. The question is more ASCII vs Unicode.

救星 2024-12-18 04:34:35

从本文http://searchsqlserver.techtarget。 com/tip/SQL-Server 中 varchar 和 nvarchar 之间的差异

VARCHAR 作为常规 8 位数据存储。但 NVARCHAR 字符串是
以 UTF-16 形式存储在数据库中 — 每个字符 16 位或两个字节,
一直 - 并转换为任何代码页正在使用
输出上的数据库连接(通常为 UTF-8)。也就是说,NVARCHAR
字符串与它们的 VARCHAR 表兄弟具有相同的长度限制 -
8,000 字节。但是,由于 NVARCHAR 每个使用两个字节
字符,这意味着给定的 NVARCHAR 只能容纳 4,000 个字符
(不是字节)最大值。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文