SQL Server Interview Question #22

What is the difference between CHAR and VARCHAR?

Data Types, Keys & Constraints Junior Beginner

Detailed Explanation

CHAR(n) stores fixed-length non-Unicode character data, while VARCHAR(n) stores variable-length non-Unicode character data.

CHAR is appropriate when values have a consistent fixed length, such as certain standardized codes. VARCHAR is generally better when string lengths vary, because it does not always reserve the full declared character length for the stored value.

For example, a CHAR(10) value is treated as fixed length, whereas VARCHAR(10) can store shorter strings more efficiently.

Code Example

DECLARE @Fixed CHAR(10) = 'SQL';
DECLARE @Variable VARCHAR(10) = 'SQL';

SELECT DATALENGTH(@Fixed) AS FixedBytes,
       DATALENGTH(@Variable) AS VariableBytes;