.NET and SQL Server interview questions - What is the difference between char (10) and nchar (10)?

 Posted by Articlesmaint on 7/20/2011 | Category: Sql Server Interview questions | Views: 6792

char nchar
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: -

You will be also interested in watching the below video: -

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response