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:
ExampleDECLARE @Name AS VARCHAR(50) = ‘MEERA’
SELECT @Name AS Name, DATALENGTH(@Name) AS LengthResult:
Name Length
MEERA 5
The Nvarchar DataType:
ExampleDECLARE @Name AS NVARCHAR(50)= ‘MEERA’
SELECT @Name AS Name, DATALENGTH(@Name) AS LengthResult:
Name Length
MEERA 10