NVARCHAR & VARCHAR

     1.difference between varchar & nvarchar

char [ (  n ) ]

Fixed-length, non-Unicode string datan defines the string length and must be a value from 1 through 8,000. The storage size is n bytes. The ISO synonym for char is character.

varchar [ (  n |  max ) ]

Variable-length, non-Unicode string datan defines the string length and can be a value from 1 through 8,000. max indicates that the maximum storage size is 2^31-1 bytes (2 GB). The storage size is the actual length of the data entered + 2 bytes.The ISO synonyms for varchar are char varying or character varying.

nchar [ ( n ) ]

Fixed-length Unicode string datan defines the string length and must be a value from 1 through 4,000. The storage size is two times nbytes. 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 can be less than the value specified for n. The ISO synonyms for nchar are national char and national character..

nvarchar [ ( n |  max ) ]

Variable-length Unicode string datan defines the string length and can be a value from 1 through 4,000. max indicates that the maximum storage size is 2^31-1 bytes (2 GB). The storage size, in bytes, is twotimes the actual length of data entered + 2 bytes. The ISO synonyms for nvarchar are national char varying and national character varying.

2.use datalength to get the storage size, you can't use LEN because LEN just counts the characters, see here: The differences between LEN and DATALENGTH in SQL Server

--to verify the length of varchar & nvarchar ()

DECLARE @n NVARCHAR(10)
DECLARE @v VARCHAR(10)


SELECT @n = 'A', @v='A'


SELECT  DATALENGTH(@n),DATALENGTH(@v)

SELECT  len(@n),len(@v)



3.Different  in MS SQL version

In MS SQL Server 2000 a row cannot exceed the size of 8000 bytes. In MS SQL Server 2000 a VARCHAR column can store up to 8000 characters and an NVARCHAR column can store up to 4000 characters. This is a limitation in MS SQL Server 2000 that has been solved in MS SQL Server 2005.


In MS SQL Server 2005 (and 2008) you can use 'MAX' in the length of a VARCHAR, NVARCHAR and VARBINARY field (VARCHAR(MAX), NVARCHAR(MAX), VARBINARY(MAX)). These data types can hold the 2 GB of data (same as BLOBs can hold).


reference: http://stackoverflow.com/questions/4713608/does-an-nvarchar-always-store-each-character-in-two-bytes

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值