Wednesday, April 23, 2014

Difference between Varchar and Nvarchar data types in SQL Server

This is really an interesting topic for me as well as for many developers who all are working on SQL Server.I have seen many developers who are quite confused in differentiating between Varchar and Nvarchar datatypes.I hope after reading my article, you will be able to answer yourself where to use Varchar and where to use Nvarchar.

So let’s see what is the main difference between Varchar and Nvarchar datatype.

There are mainly two differences between Varchar and Nvarchar data types.
Varchar data type can only store non Unicode values while Nvarchar data type can store Unicode + Non Unicode values.
Varchar data type takes 1 byte per character while Nvarchar data type takes 2 bytes per character.
Unicode Characters :- It is the International standard of a character encoding and decoding that is globally accepted.
All the characters from different languages can be encoded by Unicode standard.

Non Unicode Standard or Traditional Standard:- In earlier days when Unicode characters standard was not there for representation of a character,
We were using ASCII standard that was basically based on American and European languages encoding of characters.

Let’s take an example to understand this fact clearly.

Declare @NameNonUnicode as varchar(100)
Declare @NameUnicode as Nvarchar(100)

Set @NameUnicode='Neeraj Kumar Yadav'
Set @NameNonUnicode ='Neeraj kumar Yadav'

Select @NameNonUnicode as NonUnicode, @NameUnicode as Unicode

No comments :

Post a Comment