672

是什么意思nvarchar

SQL Server 中的charncharvarchar和有什么区别?nvarchar

4

12 回答 12

929

只是为了澄清......或总结......

  • nchar并且nvarchar可以存储Unicode字符。
  • char并且不能存储 Unicode字符。varchar
  • char并且nchar固定长度的,即使您没有用完所有空间,也会为您指定的字符数保留存储空间。
  • varchar并且nvarchar可变长度的,只会用完您存储的字符的空格。它不会像charnchar那样保留存储空间。

nchar并且nvarchar将占用两倍的存储空间,因此仅在需要Unicode支持时才使用它们可能是明智之举。

于 2008-10-06T23:02:33.887 回答
101
于 2011-11-23T23:37:48.643 回答
38

nchar 和 char 的运行方式几乎完全相同,nvarchar 和 varchar 也是如此。它们之间的唯一区别是 nchar/nvarchar 存储 Unicode 字符(如果您需要使用扩展字符集则必不可少),而 varchar 不存储。

因为 Unicode 字符需要更多存储空间,所以 nchar/nvarchar 字段占用两倍的空间(例如,在 SQL Server 的早期版本中,nvarchar 字段的最大大小为 4000)。

这个问题是这个问题的副本

于 2008-10-06T22:51:40.553 回答
34

只是为了添加更多内容: nchar - 在数据中添加尾随空格。 nvarchar - 不向数据添加尾随空格。

因此,如果您要通过“nchar”字段过滤数据集,您可能需要使用 RTRIM 删除空格。例如,名为 BRAND 的 nchar(10) 字段存储单词 NIKE。它在单词的右侧添加了 6 个空格。因此,过滤时,表达式应为: RTRIM(Fields!BRAND.Value) = "NIKE"

希望这对那里的人有所帮助,因为我刚刚挣扎了一会儿!

于 2011-07-21T05:34:35.867 回答
26

My attempt to summarize and correct the existing answers:

First, char and nchar will always use a fixed amount of storage space, even when the string to be stored is smaller than the available space, whereas varchar and nvarchar will use only as much storage space as is needed to store that string (plus two bytes of overhead, presumably to store the string length). So remember, "var" means "variable", as in variable space.

The second major point to understand is that, nchar and nvarchar store strings using exactly two bytes per character, whereas char and varchar use an encoding determined by the collation code page, which will usually be exactly one byte per character (though there are exceptions, see below). By using two bytes per character, a very wide range of characters can be stored, so the basic thing to remember here is that nchar and nvarchar tend to be a much better choice when you want internationalization support, which you probably do.

Now for some some finer points.

First, nchar and nvarchar columns always store data using UCS-2. This means that exactly two bytes per character will be used, and any Unicode character in the Basic Multilingual Plane (BMP) can be stored by an nchar or nvarchar field. However, it is not the case that any Unicode character can be stored. For example, according to Wikipedia, the code points for Egyptian hieroglyphs fall outside of the BMP. There are, therefore, Unicode strings that can be represented in UTF-8 and other true Unicode encodings that cannot be stored in a SQL Server nchar or nvarchar field, and strings written in Egyptian hieroglyphs would be among them. Fortunately your users probably don't write in that script, but it's something to keep in mind!

Another confusing but interesting point that other posters have highlighted is that char and varchar fields may use two bytes per character for certain characters if the collation code page requires it. (Martin Smith gives an excellent example in which he shows how Chinese_Traditional_Stroke_Order_100_CS_AS_KS_WS exhibits this behavior. Check it out.)

UPDATE: As of SQL Server 2012, there are finally code pages for UTF-16, for example Latin1_General_100_CI_AS_SC, which can truly cover the entire Unicode range.

于 2013-06-27T05:41:02.833 回答
15
  • char:定长字符数据,最大长度为 8000 个字符。
  • nchar: 固定长度的 unicode 数据,最大长度为 4000 个字符。
  • Char= 8 位长度
  • NChar= 16 位长度
于 2009-11-02T09:35:40.443 回答
13

nchar[(n)] (national character)

  • Fixed-length Unicode string data.
  • n defines the string length and must be a value from 1 through 4,000.
  • The storage size is two times n bytes.

nvarchar [(n | max)] (national character varying.)

  • Variable-length Unicode string data.
  • n defines the string length and can be a value from 1 through 4,000.
  • max indicates that the maximum storage size is 2^31-1 bytes (2 GB).
  • The storage size, in bytes, is two times the actual length of data entered + 2 bytes

char [(n)] (character)

  • Fixed-length, non-Unicode string data.
  • n defines the string length and must be a value from 1 through 8,000.
  • The storage size is n bytes.

varchar [(n | max)] (character varying)

  • Variable-length, non-Unicode string data.
  • n defines the string length and can be a value from 1 through 8,000.
  • max indicates that the maximum storage size is 2^31-1 bytes (2 GB).
  • The storage size is the actual length of the data entered + 2 bytes.
于 2015-02-10T14:17:50.043 回答
10

nchar(10) 是长度为 10 的固定长度 Unicode 字符串。nvarchar(10) 是最大长度为 10 的可变长度 Unicode 字符串。通常,如果所有数据值都是 10 个字符,则使用前者,后者如果长度不同。

于 2008-10-06T22:53:07.883 回答
7

nchar 需要nvarchar 更多的空间。

例如,

一个 nchar(100) 将始终存储 100 个字符,即使您只输入 5,剩余的 95 个字符将用空格填充。在 nvarchar(100) 中存储 5 个字符将节省 5 个字符。

于 2008-11-18T11:04:45.893 回答
7

区别在于:

  1. n[var]char 存储 unicode,而 [var]char 仅存储单字节字符。
  2. [n]char 需要精确长度的固定数量的字符,而 [n]varchar 接受不超过定义长度的可变数量的字符。

另一个区别是长度。nchar 和 nvarchar 都可以长达 4,000 个字符。char 和 varchar 最长可达 8000 个字符。但是对于 SQL Server,您还可以使用 [n]varchar(max) 最多可以处理 2,147,483,648 个字符。(2 GB,带符号的 4 字节整数。)

于 2009-11-02T10:33:36.410 回答
4
  • nchar 是固定长度的,可以保存 unicode 字符。它每个字符使用两个字节存储。

  • varchar 是可变长度的,不能保存 unicode 字符。它每个字符使用一个字节存储。

于 2008-10-06T22:59:43.953 回答
2

NVARCHAR可以存储 Unicode 字符,每个字符占用 2 个字节。

于 2008-10-06T22:53:14.980 回答