In SQL Server, how do I select varchar and nvarchar?

Source: Internet
Author: User

This article is from the 51cto blog (cannot stop V)

Varchar uses a single byte to store data in SQL Server. nvarchar uses Unico to store data. when a Chinese character is stored in SQL Server, it is saved as two bytes (generally Unico encoding), and an English character is saved to the database. If the field type is varchar, only one byte is occupied, if the field type is nvarchar, it occupies two bytes.

 

Normally, we can use varchar to store Chinese characters. However, if the operating system is an English operating system and the Chinese font is not fully supported, if the Chinese character is varchar in SQL Server, garbled characters (displayed as?) are displayed ??). In addition, the host normally supports Chinese environments. Therefore, if varchar is used to store data, it cannot be found in the development stage. In most cases, no problem occurs during deployment. But! If the deployed host is an English operating system and does not support the Chinese environment, the problem arises. All varchar fields are garbled when they are stored in Chinese ??). In general, you do not know that this is because you use the wrong data type for storage. You will try to install Chinese fonts and set the language environment of the operating system. These cannot solve the problem. The only solution is to set the database field type to nvarchar (or nchar ). Anyone familiar with project management should know that it is terrible to modify the database at the deployment stage. Another advantage of using nvarchar is that you do not need to consider the differences between Chinese and English characters when judging strings. Of course, using nvarchar to store English characters will increase by a factor of storage space. However, given the low storage cost, compatibility will bring you more benefits. Therefore, you should try to use nvarchar to store data during design. varchar is used only when you ensure that this field does not save Chinese characters.
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.