CHAR, VARCHAR and NVARCHAR Data Type Difference.

CHAR, VARCHAR and NVARCHAR Data Type Difference

In this sql post i will example difference between three data types Char, Varchar and Nvarchar. Before understand differences data type , first we should know what is data type.

What is Data Type in SQL?

Data type is a classification of data, which variable may contain. We have to assign proper data type to variable at the time of declaration of any variable.

The data type depend on which type of data variable can store with it.

The Char Data Type:

We use Char Datatype for store fixed length of Characters.
We can declare Char datatype like:

@Name as Char(50);

@City as Char(10)

Here, We Declare @Name variable of Char(50), means here it will allocates memory for 50 characters  and for city variable we allocate memory up to 10 character. means in name variable we can store maximum 50 character and in city variable we can store maximum 10 character.

In above statement we have declared Name variable with size 50, it is not compulsory to insert 50 character, its define the maximum capacity of variable.

We declare here Char(50) and insert only 30 character of word then only 20 characters of memory will be used and other 20 characters of memory will be wasted.

The Varchar DataType:

Varchar means variable characters and it is used to store non-unicode characters.
It will allocate the memory based on number characters inserted.
Suppose if we declared varchar(50) it will allocates memory of 0 characters at the time of declaration. Once we declare varchar(50) and insert only 10 characters of word it will allocate memory for only 10 characters.
It takes 1 bytes per Non-Unicode.
SELECT @Name AS Name, DATALENGTH(@Name) AS LengthResult:
Name        Length
MEERA      5


The Nvarchar DataType:

Nvarchar is used to store Unicode characters and it allows you to store multiple languages in database.
Nvarcahr datatype will take twice as much space to store extended set of characters as required by other languages.
It takes 2 bytes per Unicode/Non-Unicode
SELECT @Name AS Name, DATALENGTH(@Name) AS LengthResult:
Name        Length
MEERA     10

Leave a Reply

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

7 + ten =