Thursday, August 16, 2012

What is the difference between char (10) and nchar (10)?

Answer:

 

charnchar
char are fixed length data-types.nchar are also fixed length data-types.   
char does not support Unicode character.nchar support Unicode character.
char reserves 1 byte(8 bits) of memory space.nchar reserves 2 bytes (16 bits) of memory space.
char support Ansi code character, which max upto 256 character.   nchar support Unicode character, which max upto 65536 character.
char(n) specifies a length of n bytes by default.nchar(n) specifies a length of n characters by default.
char does not support character of different languages.nchar support characters of different languages.

In order to view the exact difference between the char(10) and nvarchar(10), we will use DataLength.
DataLength: -
Use DATALENGTH when the actual number of bytes is required, including trailing spaces.So, in a double-byte encoding, you'll get actual bytes, not characters.
Let us assume that we have following table with their respective fields and values.



Now let’s see what will be the DataLength of the StudentName column which is declared char (10) as datatype and what will be the DataLength of the StudentAddress column which is declared nchar(10) as datatype.
Query: -
select DATALENGTH(CustomerName) as DataLengthOfchar ,DATALENGTH(CustomerAddress) 
as DataLengthOfnchar from Customer
Output: -

No comments:

Post a Comment