---
title: "What is the difference between CHAR(n) and VARCHAR(n) in SQL?"  
description: "What is the difference between CHAR(n) and VARCHAR(n) in SQL?"  
author: "ICSM Computer"  
published: 2025-08-19  
updated: 2025-08-19  
canonical: https://www.mindstick.com/interview/34356/what-is-the-difference-between-char-n-and-varchar-n-in-sql  
category: "SQL Server"  
tags: ["sql server", "sql"]  
reading_time: 2 minutes  

---

# What is the difference between CHAR(n) and VARCHAR(n) in SQL?

#### Note:

- Use [**CHAR**](https://www.mindstick.com/articles/563/sql-data-types) when values are fixed size → faster for retrieval.
- Use [**VARCHAR**](https://www.mindstick.com/forum/189/char-varchar-nvarchar) when values vary in length → saves space.

#### CHAR(n)

- Fixed-length string.
- Always reserves **exactly** `n` **bytes**, regardless of the actual string length.
- If the value is shorter, it is **padded with spaces** to reach the defined length.
- Best used when all values are of the same or near-same length (e.g., country codes, gender).

```plaintext
CREATE TABLE TestChar (Code CHAR(5));

INSERT INTO TestChar VALUES ('ABC');
SELECT Code, DATALENGTH(Code) AS BytesUsed FROM TestChar;
```

- Stored as `'ABC '` (with 2 trailing spaces).
- `BytesUsed = 5`.

#### VARCHAR(n)

- Variable-length string.
- Stores only the actual characters entered + **1 or 2 bytes overhead** for length storage.
- More space-efficient when string lengths vary.

```plaintext
CREATE TABLE TestVarchar (Code VARCHAR(5));

INSERT INTO TestVarchar VALUES ('ABC');
SELECT Code, DATALENGTH(Code) AS BytesUsed FROM TestVarchar;
```

- Stored as `'ABC'` (no padding).
- `BytesUsed = 3`.

## Answers

### Answer by ICSM Computer

#### Note:

- Use [**CHAR**](https://www.mindstick.com/articles/563/sql-data-types) when values are fixed size → faster for retrieval.
- Use [**VARCHAR**](https://www.mindstick.com/forum/189/char-varchar-nvarchar) when values vary in length → saves space.

#### CHAR(n)

- Fixed-length string.
- Always reserves **exactly** `n` **bytes**, regardless of the actual string length.
- If the value is shorter, it is **padded with spaces** to reach the defined length.
- Best used when all values are of the same or near-same length (e.g., country codes, gender).

```plaintext
CREATE TABLE TestChar (Code CHAR(5));

INSERT INTO TestChar VALUES ('ABC');
SELECT Code, DATALENGTH(Code) AS BytesUsed FROM TestChar;
```

- Stored as `'ABC '` (with 2 trailing spaces).
- `BytesUsed = 5`.

#### VARCHAR(n)

- Variable-length string.
- Stores only the actual characters entered + **1 or 2 bytes overhead** for length storage.
- More space-efficient when string lengths vary.

```plaintext
CREATE TABLE TestVarchar (Code VARCHAR(5));

INSERT INTO TestVarchar VALUES ('ABC');
SELECT Code, DATALENGTH(Code) AS BytesUsed FROM TestVarchar;
```

- Stored as `'ABC'` (no padding).
- `BytesUsed = 3`.


---

Original Source: https://www.mindstick.com/interview/34356/what-is-the-difference-between-char-n-and-varchar-n-in-sql

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
