Char and varchar in SQL Server

Source: Internet
Author: User

Data Types of fixed-length (char) and variable-length (varchar) characters

Char [(n)]

Character data with a fixed length of n Bytes and is not Unicode. N must be a value between 1 and 8,000. The storage size is n Bytes. The synonym for char in the SQL-92 is character.

Varchar [(n)]

Variable-length and non-UNICODE character data with a length of n Bytes. N must be a value between 1 and 8,000. The storage size is the actual length of the input data bytes, rather than n Bytes. The length of the input data can be zero. The synonym for varchar in the SQL-92 is Char varying or character varying.

Char is of a fixed length, so it is much faster than varchar! HoweverProgramIt is a little troublesome to handle. We need to remove spaces on both sides using functions such as trim!

Note

If n is not specified in the data definition or variable declaration statement, the default length is 1. If n is not specified by the cast function, the default length is 30.

The default database sorting rules will be assigned to objects using Char or varchar unless a specific sorting rule is assigned to the Collate clause. This sorting rule controlsCodePage.

For websites that support multiple languages, Unicode nchar or nvarchar data types should be considered to minimize character conversion issues. If Char or varchar is used:

If you want the data values in the column to be close to the same size, use char.

If you want the data values in the column to be significantly different, use varchar.

If set ansi_padding is off when you execute create table or alter table, a char column defined as null will be processed as varchar.

When the collation code page uses double-byte characters, the storage size is still n Bytes. Depending on the string, the storage size of n Bytes may be less than n 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.