What is Different Between CHAR & VARCHAR ?

VARCHAR Datatype: 

Varchar is a datatype in SQL that holds characters of variable length. This data type stores character strings of up to 255 bytes in a variable-length field. If the length of the string is less than set or fixed-length then it will store as it is without padded with extra blank spaces. The storage size of the VARCHAR datatype is equal to the actual length of the entered string in bytes. Its slower than CHAR and uses dynamic memory allocation.

Example:

CREATE TABLE Student(Name VARCHAR(50), Department CHAR(20));

INSERT into Student VALUES(‘Raj’, ‘BCA’);

SELECT LENGTH(Name) FROM Student;

Output –

5

 CHAR Datatype: 

CHAR is a data type in SQL that can store characters of a fixed length. A data type defines the type of data that a declared variable can hold. The storage size of the CHAR datatype is n bytes(set length). We should use this datatype when we expect the data values in a column are of the same length. 

Example:

CREATE TABLE Student(Name VARCHAR(20), Department CHAR(20));

SELECT LENGTH(Department) FROM Employee;

Output –

20

What is Different Between CHAR & VARCHAR datatypes :

CHAR :

  1. CHAR datatype is used to store character strings of fixed length.
  2. CHAR stands for Character.
  3. Char datatype can be used when we expect the data values in a column to be of same length.
  4. It stores values in fixed lengths and are padded with space characters to match the specified length.
  5. It can hold a maximum of 255 characters.
  6. We should use the CHAR datatype when we expect the data values in a column are of the same length.
  7. It uses static memory allocation.

VARCHAR :

  1. VARCHAR datatype is used to store character strings of variable length.
  2. VARCHAR stands for Variable Character.
  3. Varchar datatype can be used when we expect the data values in a column to be of variable length.
  4. VARCHAR stores values in variable length along with 1-byte or 2-byte length prefix and are not padded with any characters.
  5. It can hold a maximum of 65,535 characters
  6. We should use the VARCHAR datatype when we expect the data values in a column are of variable length
  7. It uses dynamic memory allocation.

Leave a Reply