SQL Server data type Learning

Source: Internet
Author: User
These days, we need to port Oracle to SQL Server, use Microsoft SQL Server migration assistant 2005 for Oracle, and use the data type, so I had a comprehensive learning on the Data Types of SQL Server. I would like to summarize several key points and share them with my friends.

The difference between char and nchar

Char is a fixed-length byte, varchar is a variable-length byte, and nchar is a Unicode-encoded fixed-character length.
For example, 12345 is stored in char (6) with a space of 123456, 6 bytes in total.
In China, char (6) is stored in China with two spaces, 6 bytes in total.
12345 is stored in varchar (6) as 123456 in a total of 5 bytes.
In China, varchar (6) is stored in China, with 4 bytes in total.
Nchar (6) is the same for Chinese characters and halfwidth characters, while nchar is similar to Char and adopts the Unicode standard character set. Each character occupies two bytes.

The difference between char and varchar

The Char data type is used to store non-uniform encoded data with a fixed length. When defining a column of this type, you must specify the column length. This data type is useful when you always know the length of the data to be stored. For example, when you store data in zip code and 4 character format, you know that it always takes 10 characters. The column width of this data type is up to 8000 characters;

The varchar data type, same as the char type, is used to store non-uniform encoding character data. Unlike char, the data type is variable. When defining a column as the data type, you must specify the maximum length of the column. The biggest difference between it and char is that,The storage length is not the column length, but the data length.

The difference between varchar and nvarchar

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.
Nvarchar (N)
Unicode data with a variable length of n characters. The value of N must be between 1 and 4,000. The storage size of bytes is twice the number of input characters.

The two fields have Field Values: Me and coffee.
The varchar field occupies 2 × 2 + 6 = 10 bytes of storage space, while the nvarchar field occupies 8 × 2 = 16 bytes of storage space.
If the field value is only in English, you can select varchar. If the field value contains many double-byte (Chinese, Korean, etc.) characters, use nvarchar.

That's all.

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.