Difference Between CHAR and VARCHAR in SQL
| Feature | CHAR(n) | VARCHAR(n) |
|---|---|---|
| Meaning | Fixed-length string | Variable-length string |
| Storage | Always exactly n characters | Only stores the actual length used (up to n) |
| Padding | Pads with spaces if shorter than n | No padding — stores only the actual characters |
| Performance | Slightly faster for fixed-size values | More storage-efficient for variable sizes |
| Use Case | When all values are about the same length | When values vary widely in length |
| Example | CHAR(5) always stores 5 characters | VARCHAR(5) stores 1–5 characters |
1. CHAR(n) — Fixed Length
- Always exactly
ncharacters. - If the input is shorter, it’s padded with spaces.
- Faster for fixed-length data because the database knows exactly how much space each value takes.
✅ Example:
CREATE TABLE ExampleChar (
code CHAR(5)
);
Insert:
INSERT INTO ExampleChar (code) VALUES ('A1');
| code |
|---|
A1 |
Always takes 5 bytes (assuming 1 byte per character, like in ASCII).
2. VARCHAR(n) — Variable Length
- Stores only the characters you provide (up to
n). - No extra spaces.
- More storage efficient when lengths vary a lot.
✅ Example:
CREATE TABLE ExampleVarchar (
code VARCHAR(5)
);
Insert:
INSERT INTO ExampleVarchar (code) VALUES ('A1');
| code |
|---|
A1 |
Takes only 2 bytes for A1, plus 1–2 bytes extra to store the length internally (depends on DB engine).
When Should You Use CHAR vs VARCHAR?
Use CHAR When | Use VARCHAR When |
|---|---|
| Data is always fixed length | Data is variable length |
Example: country codes (US, FR) | Example: names, emails, descriptions |
| Speed is a tiny bit more important | Storage efficiency is important |
| Minor gain for huge tables | Better if length varies a lot |
Real-World Example
| Field | Type | Why |
|---|---|---|
| Country code | CHAR(2) | Always 2 characters — US, FR, JP. |
| User email | VARCHAR(255) | Varies a lot — from short to very long addresses. |
| Phone number | CHAR(10) | Always 10 digits (in some countries). |
| Address | VARCHAR(500) | Highly variable. |
In Short
CHAR(n): Fixed-length string — alwaysncharacters, padded with spaces.VARCHAR(n): Variable-length string — stores exactly what you insert, no padding.