What is the difference between varchar and nvarchar in SQL?

Source: Internet
Author: User

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.

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.

Two fields have field values: me and coffee.
The varchar field accounts for 2x2+6=10 bytes of storage, while the nvarchar field accounts for 8x2=16 bytes of storage space.

If the field value is only English can choose varchar, and the field value exists more double-byte (Chinese, Korean, etc.) characters with nvarchar

Above is a summary of the introduction, through the above introduction, you can know.

varchar (4) You can enter 4 letters or two characters

nvarchar (4) can lose four characters, can also lose 4 letters, but a maximum of four
The difference between char, varchar, nchar, nvarchar

For the string field in the program, SQL Server has char, varchar, nchar, nvarchar four types to correspond (temporarily regardless of text and ntext), open the database, the four types are often blurred, here do a comparison.

Fixed or variable length
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; var prefixes indicate that the actual storage space is variable, such as Varchar,nvarchar variable length character data will not be filled with spaces , the exception is that the text store is also variable length.
Unicode or non-Unicode
In a database, English characters require only one byte to store, but Chinese characters and many other non-English characters require two bytes of storage. If the English and Chinese characters exist simultaneously, because of the different occupied space, it is easy to cause confusion, resulting in the reading of the string is garbled. The Unicode character set is created to address the incompatibility of the character set, and all of its characters are represented in two bytes, meaning that the English character is also represented in two bytes. The prefix n represents Unicode characters, such as Nchar,nvarchar, which use the Unicode character set.
Look at the field capacity based on the two points above
Char,varchar up to 8,000 English, 4,000 kanji
Nchar,nvarchar can store 4,000 characters, whether English or Chinese
Use (personal preference)
If the amount of data is very large and can be 100% to determine the length and save only ANSI characters, Char
Can determine the length is not necessarily ANSI characters or, then use nchar;
For oversized data, such as article content, use ntext
Other general-purpose nvarchar
Character comparison of char, varchar, nchar and nvarchar

CHAR
Char is convenient for storing fixed-length data, and the index on a char field is highly efficient, such as defining char (10), which takes up 10 bytes of space regardless of whether the data you store is 10 bytes.
VARCHAR
Store variable length data, but the storage efficiency is not high, if the value of a field may be not fixed length, we only know it cannot exceed 10 characters, it is the most advantageous to define it as VARCHAR (10). The actual length of the varchar type is +1 of the actual length of its value. Why "+1"? This byte is used to hold the length that is actually used.
From the space consideration, with the varchar suitable, from the efficiency consideration, uses the char to be suitable, the key is to find the tradeoff point according to the actual situation.

The

Text
Text stores non-Unicode data of variable length, with a maximum length of 2^31-1 (2,147,483,647) characters. The three types of
NCHAR, NVARCHAR, NTEXT
have a "N" more than the first three. Compared to char and varchar, nchar and nvarchar store up to 4,000 characters, whether in English or Chinese characters, while char and varchar can store up to 8,000 English and 4,000 Chinese characters. It can be seen that the use of nchar, nvarchar data types without worrying about the input characters are English or Chinese characters, more convenient, but in the storage of English number of some losses.
so generally, if it contains Chinese characters, use Nchar/nvarchar, if pure English and numbers, with Char/varchar.

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.