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

The Essential Guide to Cryptocurrency Exchange Platform Development for Beginners and Beyond

Introduction Cryptocurrencies took the world by storm, setting up a new financial system and breaking…

5 months ago

Top 20 System Administrator Interview Questions and answers

In my previous article I have given Top 20 technical support interview questions with its…

8 months ago

Desktop Support Scenario Based Interview Questions

In my previous articles I have given 15 most asked desktop support interview questions with…

8 months ago

How Do Business Analysts Use SQL for Their Needs?

A business analyst is someone who is versed in processes of data analysis used for…

8 months ago

Top 15 Control-M Interview Questions with Answers

In my previous article I have already given top questions and answers for Desktop support…

8 months ago

Top 20 SQL Interview Questions for Production Support

In my previous article I have given unix production support interview questions and answers. In…

8 months ago