Difference between varchar and nvarchar in sql.

Hi i am explaining the major difference between the VARCHAR and NVARCHAR Data Type in Sql Server:

Varchar

Non-Unicode Variable Length character data type.
Example:

DECLARE @Name AS VARCHAR(50) =‘PAWAN SHUKLA’
 SELECT @Name

It takes 1 byte per character

Example:
 DECLARE @Name AS VARCHAR(50) = ‘PAWAN SHUKLA’
 SELECT @Name AS Name,
 DATALENGTH(@Name) AS Length

Result:
 Name Length
 PAWAN SHUKLA 12

Optional Parameter n value can be from 1 to 8000.Can store maximum 8000 Non-Unicode characters.

NVarchar

UNicode Variable Length character data type. It can store both non-Unicode and Unicode (i.e. Hindi, English etc) characters.
Example:

DECLARE @Name AS NVARCHAR(50)= ‘PAWAN SHUKLA’
 SELECT @Name

It takes 2 bytes per Unicode/Non-Unicode character.

Example:
 DECLARE @Name AS NVARCHAR(50)= ‘PAWAN SHUKLA’
 SELECT @Name AS Name,
 DATALENGTH(@Name) AS Length

Result:
 Name Length
 PAWAN SHUKLA 24

Optional Parameter n value can be from 1 to 4000.Can store maximum 4000 Unicode/Non-Unicode characters

Leave a Reply

Your email address will not be published. Required fields are marked *