The main difference between CHAR and VARCHAR in SQL lies in how they store data and handle storage space.
CHAR is a fixed-length data type, while VARCHAR is a variable-length data type.
For example, if I define a column as CHAR(10) and store the value 'CAT', SQL will still reserve 10 characters of space — it’ll store 'CAT ' (with trailing spaces). On the other hand, if I use VARCHAR(10), it’ll only use 3 characters of space for 'CAT', saving storage.
So, I usually choose:
CHARfor columns where the data length is always consistent, like gender codes ('M','F'), country codes ('IND','USA'), or fixed-format identifiers.VARCHARfor columns where the data length varies, like names, addresses, or descriptions.
Here’s a small example:
CREATE TABLE Example (
Code CHAR(5),
Name VARCHAR(50)
);
In one of my projects, we had a table for storing product codes and descriptions. Product codes were always 8 characters, so I used CHAR(8). But for product descriptions, which varied in length, I used VARCHAR(200) to save space.
A challenge I’ve faced with CHAR is that when comparing or inserting values, trailing spaces can sometimes cause unexpected results, especially across different databases.
Performance-wise, CHAR can be slightly faster for fixed-length data because it’s predictable in size, but for large text or inconsistent-length data, VARCHAR is definitely more efficient.
So in short —
CHAR= fixed length, uses more space but faster for uniform data.VARCHAR= variable length, space-efficient and flexible for varied data.
