What is Difference Between Char and varchar | Examples of Char and Varchar

In my previous articles i have given information about the different datatypes in SQLIn this article I will try to give you difference between char and varchar with some examples .Before giving the difference in tabular format i would like to explain about char and varchar datatypes with its usages.Then i will explain the Difference between char and varchar in detail.

What is Char datatype with example :

In this section i would like to give you information about character datatype with its syntax and example. This will give you clear idea about where to use the Char datatype.

Purpose of Char Datatype :

The char datatype is used where user needs to deal with fixed length strings.

Syntax and example :

variable_name Char(Size);

The default size of Char datatype is 1 and maximum size of the char datatype is 254.

Real Life Example :

Create a table with Title which has fixed length as 2 and name as varchar2

Create table Employee

(Title Char(2),Employee_name Varchar2(30));

This will create Employee table with Char datatype.

What is Varchar datatype with example :

In this section i would like to give you information about varchar datatype with real life examples.This will give you clear idea about where to use varchar datatype in detail.

Purpose of Varchar datatype (asked by reader Priyanka Sarkar) :

The varchar datatype is used where user needs to deal with variable length data or string.

Syntax and example :

variable_name Varchar(Size);

The Length is unsigned integer constant.The maximum length for varchar2 is 32672.

Difference between Char and Varchar

Real Life Example :

Create a table with Title which has fixed length as 2 and name as varchar2

Create table Employee

(Title Char(2),Employee_name Varchar2(30));

This will create Employee table with Char datatype.

Difference between Char and Varchar in Tabular format :

In this section I would like to give you difference between Char and Varchar in table format.SQL supports the 2 types of datatypes for dealing with character.One datatype is for fixed length string and other datatype is for variable length string.If you get the difference between char and varchar with point-wise table format it will be easy for user to use that datatype properly.

Char Datatype Varchar Datatype
The Char datatype is used where the length of string is fixed. The Varchar datatype is used where the length of string is variable or not fixed.  
The maximum size for char datatype is 254. The maximum size for varachar datatype is 32672
The Size of char datatype is always fixed. Char(2) will be fixed length 2. The Size of Varchar datatype is variable length.
It will allow the trailing spaces. The trailing spaces are not applied in varchar datatype.
A CHAR(10) field will store “Amiet” as 10 bytes by appending 5 trailing spaces. A VARCHAR2(10) field will store “Amiet” as 7 bytes (assuming 2 bytes to store length).
There is not Char2 datatype The Varchar2 datatype is there which has maximum 4000 bytes of size
User can use alternative name as :  Character(n) User can use alternative name as : Character Varying(n)

I am hoping that you will get actual difference between Char and varchar with real examples. If you like this article or if you have any issues with the same kindly comment in comments section.

4 Replies to “What is Difference Between Char and varchar | Examples of Char and Varchar”

  1. Thank you Amit, for all the things you have done for us . You deserves the highest reward in this world!

Comments are closed.