The difference between char, varchar, nchar, nvarchar in the "string difference" SQL Server:

Source: Internet
Author: User

First, the definition

Char: fixed length, storing ANSI characters, insufficient complement English half-width space.

NCHAR: fixed length, storing Unicode characters, insufficient complement to English half-width space

VARCHAR: variable length, storing ANSI characters, automatically changing according to data length.

NVARCHAR: variable length, storing Unicode characters, automatically changing according to data length.

nvarchar (n): A variable-length Unicode character data that contains n characters. The value of n must be between 1 and 4,000. The storage size of bytes is twice times the number of characters entered. The input data character length can be zero.

varchar[(n)]: variable-length, non-Unicode character data with a length of n bytes. n must be a numeric value between 1 and 8,000. Storage size is the actual length of bytes of input data, not n bytes. The input data character length can be zero.

Note: ANSI mainly stores data in single byte, which is generally suitable for English. Our commonly used Chinese characters need to be stored in two bytes, so we will use Unicode data type, otherwise the data read out may be garbled.

Second, the difference

① from the storage mode, nvarchar is stored by character, while varchar is stored in bytes;

② from storage, varchar saves space because the storage size is the actual length of bytes, while nvarchar is a double-byte storage;

③ in use, if the storage contents are English characters and no Chinese characters and other language symbols, the proposed use of varchar, including the use of Chinese characters nvarchar, because nvarchar is the use of Unicode encoding, that is, a unified character encoding standard, will reduce the chance of garbled;

④ If you do a project that may involve conversion between different languages, it is recommended to use nvarchar.

Iii. Advantages and Disadvantages

Nvarchar advantages: When judging the string can not consider the difference between the two characters in English, you can avoid the program garbled problem.

Disadvantage: Storing English characters will increase storage space by one more times. But given the low cost of storage, prioritizing compatibility gives you more benefits, without the efficiency of a varchar high.

Iv. why use nvarchar?

with n prefixes, n denotes Unicode characters, that is, all characters account for two bytes, Nchar,nvarchar

Character, the English characters only need one byte storage is sufficient, but the Chinese character is numerous, requires two bytes of storage, English and Chinese characters are prone to confusion, the Unicode character set is to solve the problem of incompatible character sets, all of its characters are represented by two bytes, That is, the English character is also represented in two bytes.

V. A brief introduction to Var

With the Var prefix, it means that the actual storage space is getting longer, Varchar,nvarchar

The so-called length is fixed, when the input length of the data does not reach the specified length will be automatically filled with English space after it, so that the length of the corresponding length, and the variable length character data will not be filled with spaces, the exception is that the text storage is also variable length.

Vi. How do I use these types?

If you are sure of the length of the stored data and do not pack in Chinese, you can choose the char type.

If you are sure of the stored data length, but may include Chinese, you can choose the nchar type.

If you are unsure of the length of the stored data, store only English, the best use of the number varchar

If you are unsure of the length of the stored data, it is possible to have Chinese, you can choose the nvarchar type, and in SQL Server2005 is the more commonly used character data type.

The difference between char, varchar, nchar, nvarchar in the "string difference" SQL Server:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.