Note: Use nchar, nvarchar, and ntext in SQL Server

Source: Internet
Author: User
Tags sql server books

I squatted in the garden every day to see others. I haven't written any new articles for a long time ~~ Let's not throw bricks!

To put it bluntly, when my colleague helped the customer record data the day before yesterday, he found that one person's name had a word "Jie" (I .e., Jie, however, I couldn't use sogou pinyin or hichina. I tried Baidu and found a suggestion post on the sogou Forum. Someone suggested adding"

 

"Text, the following person posted the word" yellow.

Now that the word has been found, I thought everything was okay. I copied it to feiqiuli and sent it to my colleagues. It turned out to be "?"! What is the situation of magic horse?

I immediately flipped through the post and saw the reply from the Forum moderator below:

 

It seems to be a coding problem. I copied it to a new notepad and the prompt was displayed during saving:

 

It is a coding problem! At this time, I suddenly thought that the field storing the name uses the varchar type. Can it store this word? A try is also a question mark! Use the nvarchar type field to store the data. It is no problem to be decisive. Check the SQL Server books online and see the differences between varchar and nvarchar:

Except in the following cases,Nchar,NvarcharAndNtextAndChar,VarcharAndTextUse the same:

    • Unicode supports a wider range of characters.
    • Storing Unicode characters requires more space.
    • NcharThe maximum size of a column is 4,000 charactersCharAndVarcharThey are 8,000 characters different from each other.
    • Use the maximum specifier,NvarcharThe maximum column size is 2 ^ 31-1 bytes. RelatedNvarchar (max)For more information, see use the volume data type.
    • Unicode constants start with N and specify: n' a unicode string '.
    • All Unicode data uses the character set defined by the Unicode standard. Unicode sorting rules used for Unicode columns are based on the following attributes: Case Sensitive, accent sensitive, Kana sensitive, full-width and binary.

I have always used varchar to create tables in SQL Server. I always feel that nvarchar has the same length and Character Count in addition to storing Chinese characters, and there is basically no need to use it. This lesson has been learned. If you enter personal names or other fields that may not contain uncommon characters in GBK, you 'd better use nchar, nvarchar, or ntext to avoid further program pitfalls.

PS: Thanks to dreamhappy, when using SQL statements to insert or update data in nvarchar columns, it is best to add n before the string, for example, insert into table () values (N 'hour'). Otherwise, the characters exceeding GBK will still be displayed as "?" . If anyone can save n writing, please comment. Thank you!

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.