Difference Between Sql Server CHAR, VARCHAR and NVARCHAR Data Type

Difference Between Sql Server CHAR, VARCHAR and NVARCHAR Data Type

CHAR is a Fixed Length Data Type. For example if you declare a variable/column of CHAR (10) data type, then it will always take 10 bytes irrespective of whether you are storing 1 character or 10 character in this variable or column. And in this example as we have declared this variable/column as CHAR(10), so we can store max 10 characters in this column.

OR

It stores fixed length of character. if you declare char(50) then it allocates memory for 50 characters. if you store 10 character word then it store it in 10 characters memory location and other 40 character’s memory location will be wasted.

Char datatype can store upto 8000 bytes of fixed-length character.

VARCHAR is a variable length Data Type. For example if you declare a variable/column of VARCHAR (10) data type, it will take the no. of bytes equal to the number of characters stored in this column. So, in this variable/column if you are storing only one character then it will take only one byte and if we are storing 10 characters then it will take 10 bytes. And in this example as we have declared this variable/column as VARCHAR (10), so we can store max 10 characters in this column.

OR

Varchar means variable characters. It allocates memory as per data stored into this. If you specify varchar(50) at the time of declaration then it allocates 0 memory location at the starting if you specify, it can be null. When you store 10 characters word it will allocate memory for 10 characters and store into that. So there will be no memory loss. It stores only non-unicode characters.

Varchar datatype can store upto 8000 bytes of variable-length character.

 

Example : Between Char and Varchar

DECLARE @CharName Char(20) = ‘DOTNETTRK’,

@VarCharName VarChar(20) = ‘DOTNETTRK ‘

SELECT DATALENGTH(@CharName) CharSpaceUsed,

DATALENGTH(@VarCharName) VarCharSpaceUsed

Result:

CharSpaceUsed VarCharSpaceUsed

————- —————-

20            9

 

 NVarchar means unicode variable characters. it allocates memory as same as varchar. But It stores unicode characters.

Nvarchar datatype can store up to 4000 bytes of variable-length unicode character data.

 

Example : Between Varchar and NVarchar

Varchar:

It takes 1 byte per character

DECLARE @FirstName AS VARCHAR(50) =‘DOTNETTRK’
SELECT @FirstName ASFirstName,DATALENGTH(@FirstName) ASLength
Result:
FirstNameLength
DOTNETTRK 9

 

If Optional parameter value nis not specified in the variable declaration or column definition then it is considered as 1.

DECLARE @firstName VARCHAR=‘DOTNETTRK’
SELECT @firstName FirstName,DATALENGTH(@firstName)Length

Result:
FirstName Length
D 1

 

NVarchar:

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

DECLARE @FirstName AS NVARCHAR(50)=‘DOTNETTRK’
SELECT @FirstName ASFirstName,DATALENGTH(@FirstName) ASLength

Result:
FirstName Length
DOTNETTRK 18

 

 

If Optional parameter value n is not specified in the variable declaration or column definition then it is considered as 1.

DECLARE @firstName NVARCHAR=‘DOTNETTRK’
SELECT @firstName FirstName,DATALENGTH(@firstName)Length

Result:
FirstName Length
D 2

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply