Research on SQL Server Character Set

Source: Internet
Author: User
Aside from commonly used temporary tables, read only the formal tables, and add the maximum length of the varchar and nvarchar types. The following experiences are obtained:
<1> maximum length
1. varchar is used in the Chinese character set. The maximum length is 8000. The value 8000 refers to the number of bytes (datalength (). That is, the maximum length is 8000 English characters and 4000 Chinese characters;
Special: If the CHARACTER n 'A' is saved, it can be up to 4000 characters, but the space occupied is 4000 bytes;
2. nvarchar is used in the Chinese character set. The maximum length can be 4000 characters. This 4000 character set refers to the number of characters (LEN (), that is, the maximum length can be 4000 English characters and 4000 Chinese characters;

3. varchar is used in the English character set. The maximum length is 8000. The value 8000 refers to the number of bytes (datalength (). Since both Chinese and English characters are saved as 1 byte, therefore, up to 8000 English and Chinese characters can be saved;
4. nvarchar is used in the English character set. The maximum length can be 4000 characters. This 4000 character set refers to the number of characters (LEN (), that is, the maximum length can be 4000 English characters and 4000 Chinese characters;
<2> text display problems
1. N ''must be used with the data types nvarchar and nchar. If n'' is mandatory for the varchar and char fields, special phenomena may occur and even uncontrollable;
2. In the English character set, to save double-byte characters such as special characters and Chinese characters, you must use nvarchar or nchar to define the table structure and use n'' to save the data '';
3. Under the Chinese character set, the database system can save double-byte characters such as special characters and Chinese characters by default. That is, if n ''is not used, it is processed in double bytes. However, it is recommended that:
If nvarchar or nchar is used when defining the table structure, n'' is used for saving '',
If you use varchar and char when defining the table structure, do not use n;
4. substring (expression, start, length)
Length: an integer that refers to the length of the substring (number of characters to return or the number of bytes ).
The number of characters in the Chinese character set;
In the English character set, char and varchar are obtained by the number of bytes, and nchar and nvarchar are obtained by the number of 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.