Difference between Char and Varchar

Key Difference: In database systems like SQL Server, Char and Varchar are both datatypes, where char actually refers to character and Varchar refers to variable character. They both are used to store string type of values with a maximum length of 8,000 characters. The storage size of char is the same as declared whereas the stage sixe of Varchar depends upon the bytes of the actual data entered.

Char is a fixed length data type, where Varchar is a variable length data type. While using char it only uses those many bytes to store data which are mention in defining a char value. For example char(7) will take only 7 bytes in order to store data. It is important to mention that this condition also includes space. On the other hand, a Varchar takes a variable space, which means that it will take only that much space as the actual value entered in as the data for Varchar datatype.

Varchar takes  1 or 2 bytes extra in addition to 1 byte per character. These additional bytes are used for holding information about the length. Both are also used similarly in order to introduce an item. It is always preferred to use char when the data entered in a column is likely to be of the same size as is expected to be entered.

Let us take an example – if a Char data is declared in a way: declare test Char(20), and test= "testing", then the testing will occupy first 7 bytes and rest will be filled with blank data. On the other hand, if a Varchar data is declared in a way: declare test Varchar(20), and test = “testing”, then it will occupy only 7+2 bytes.

Char should be used when the length of the variable is known, whereas Varchar should be used only when the length is unknown. Char is faster than Varchar, as Varchar system spends some time for detecting the time for finding out the end of the string. On the other hand, this time is not wasted in Char.

Comparison between Char and Varchar:

 

Char

Varchar

Full Form

Character

Variable Character (in context to character of variable length)

Meaning

Used to store non-Unicode string data of fixed length

Used to store non-Unicode string data of variable length

Bytes used for storage

1 byte per character

1 byte per character and 1 or 2 bytes additional for holding length information

Application

Used for storing data like Phone number, etc. (data entries are consistent.)

Used for storing data like Address (data entries vary largely)

Condition: input string less than declared bytes

The space without any character will be padded with space characters

The space without any character will not be padded with any of the characters

Condition: input string more than declared bytes

The string will be truncated to declared bytes

The string will be truncated to declared bytes.

Image Courtesy: functionx.com, vbforums.com

Most Searched in Business and Finance Most Searched in Entertainment and Music
Most Searched in Electronics Most Searched in Arts and Humanities
Fort vs Fortress
Income vs Profit
Journal vs Ledger
Alright vs All Right

Add new comment

Plain text

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.