Categories: SQL Tutorials

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.

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.

Amit S

Oracle Consultant with vast experience in Oracle BI and PL/SQL Development. Amiet is the admin head of this website who contributes by preparing tutorials and articles related to database technologies. He is responsible to manage the content and front-end of the website.

Recent Posts

What is Root Cause Analysis (RCA) With real examples

In my previous article I have given details about application support engineer day to day…

2 months ago

Application Support Engineer Day to day responsibilities

In my previous articles I have given the roles and responsibilities of L1,L2 and L3…

2 months ago

What is mean by SLA ( Service Level Agreement) with Examples?

In my previous articles i have given the hierarchy of production support in real company…

2 months ago

What is Production support Hierarchy in organization?

In this article i would like to provide information about production support organization structure or…

2 months ago

What are roles and responsibilities for L3 Support Engineer?

In my previous article I have given roles for L1 and L2 support engineer with…

2 months ago

What are roles and responsibilities of L2 Engineer?

I have started this new series of how to become application support engineer. This article…

2 months ago