Exclusive CHAR Vs. VARCHAR in DB2 SQL

Here’re the differences between CHAR and VARCHAR. You can use these to store strings. For instance, names are one such kind of data.

CHAR Vs VARCHAR

CHAR data type

The CHAR data type pads space when the moved string is shorter. Let us say you have created emp_name with CHAR of 20.

emp_name CHAR(20)

Here, I moved ‘Vani Mohan’ to the emp_name. The first 10 bytes will have actual string, and the next 10 bytes will be spaces.

Char vs Varchar

VARCHAR data type

The VARCHAR works the other way round of CHAR. Let us say the emp_name column you defined as VARCHAR(20).

When you move ‘Vani Mohan’ to the emp_name, it stores the string as two parts. One is length, and another part is an actual string.

==> Length (2 bytes) + Actual string

When you move 'VANI MOHAN' to emp_name. It occupies internally as 
first 2 bytes is length and rest 10 bytes will be actual length.

The VARCHAR doesn’t pad spaces when the string length supplied is shorter.

Performance

The CHAR and VARCHAR improve performance if you use them correctly.

Related posts

Top organic products

For healthy and stylish life style.

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.