Differences between nvarchar and varchar in SQL Server

Source: Internet
Author: User
Tags numeric value

first, let's look at the instructions in the official help of nvarchar and varchar:
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. The synonym for varchar in SQL-92 is char varying or character varying.

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. The synonyms for nvarchar in SQL-92 are national char varying and national character varying.


Second, how to choose two kinds of characters?
both varchar and nvarchar can store Chinese characters. The difference is that a Chinese character occupies a varchar (2), only nvarchar (1), and the letter is only the varchar (1), then in the database field for the length of time, with varchar you do not necessarily know it exactly how many words, if used nvarchar, Then the Chinese character is also nvarchar (1), the letter is also nvarchar (1), then already very obvious.
at the same time, varchar retrieval is faster than nvarchar.
varchar uses a single byte to store data in SQL Server, and nvarchar uses Unico to store data. Chinese characters stored in SQL Server are saved as two bytes (typically with Unico encoding), English characters are saved to the database, and if the field's type is varchar, only one byte is consumed, and two bytes if the field is of type nvarchar. Under normal circumstances, we can also store Chinese characters using varchar, but if the operating system is an English operating system and the support is not comprehensive, the text character in SQL Server store is varchar is garbled (shown as??). And under normal circumstances, the host will support the Chinese environment, so if the use of varchar to store data, in the development phase is not found. In most cases, there will be no problem at the time of deployment.
but! If the host computer is an English operating system and does not support the Chinese environment, then the problem comes out. All varchar fields are garbled when they are stored in Chinese (shown as??). ). And generally you don't know this because you're using the wrong data type to store the resulting, you'll try to install the Chinese font, try to set the operating system's language environment ... None of this solves the problem, and the only solution is to nvarchar (or nchar) the type of database field. More familiar with the project management of friends should know, to the deployment stage to modify the database is a very scary thing.
Another good thing about using nvarchar is that you don't need to consider the difference between the two characters when judging a string.
of course, using nvarchar to store English characters will increase storage space by one more times. But given the low cost of storage, prioritizing compatibility gives you more benefits.
therefore, when design should try to use nvarchar to store data. Use varchar to store only if you are sure that the field is not saved in Chinese.

Reprinted from: http://114267913.blog.163.com/blog/static/22807058201011045613922/

Differences between nvarchar and varchar in 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.