SQL Server database encoding (differences between varchar and nvarchar)

Source: Internet
Author: User

Let's just start with an example.

Open SQL Server to create a table:

Create Database testdb

Go

Create Table Test
(
Name1 varchar (10 ),

Name2 nvarchar (10)
)

Go

Insert into test values ('february 5 ', 'february 5, 80 or 90')-success

Insert into test values ('february 5, 80 or 90, ') -- the first field fails to be inserted.

Insert into test values ('february 5 ', 'february 5, 1234, 1234, 80 or 90 1') -- the second field fails to be inserted.

The first field can be inserted into five Chinese Characters

The second field can insert 10 Chinese characters for multiple

Bytes ------------------------------------------------------------------------------------

Insert into test values ('20140901', '20160901') -- Success

Insert into test values ('20140901', '20160901') -- the first field fails to be inserted.

Insert into test values ('20140901', '20160901') -- the second field fails to be inserted.

(Insert letters and ASCII characters. The result is the same)

The first field can insert up to 10 ASCII codes.

The second field can insert 10 ASCII codes for multiple

Bytes ---------------------------------------------------------------------------------------

Insert into test values ('one, Two, Three aaa', 'february 80 or 90, ') -- Success

Insert into test values ('2014, 2, 3 aaaab', '2014, 5, 6, 7, 80 or 90 ') -- the first field fails to be inserted.

Insert into test values ('one, Two, Three aaa', 'August 5, AB ')-success

Insert into test values ('one, Two, Three aaa', 'february, abc') -- insertion of the Second Field failed.

Bytes --------------------------------------------------------------------------------------

Conclusion: up to 10 varchar-type ASCII files and up to 5 Chinese characters can be saved.

Up to 10 ASCII and Chinese characters of the nvarchar type can be saved.

Note: The varchar ASCII character occupies one byte, and the Chinese character occupies two bytes.

The nvarchar type always occupies 2 bytes for one character.

And:

Create Table Test
(
Name1 varchar (10), which occupies 10 bytes of space

Name2 nvarchar (10) -- occupied 20 bytes
)

Verification:

Select * from test

Select name1, Len (name1), name2, Len (name2) from test

 

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.