liveBook · Manning

Char(n)

As explained in 100 SQL Server Mistakes and How to Avoid Them

CHAR is a SQL Server fixed-length, non-Unicode character data type that stores one byte per character and pads values to the declared length.

Mistake #7: treating CHAR(10) and VARCHAR(10) as interchangeable. Fixed-length CHAR values are padded with whitespace to the declared length, so a shorter value still occupies the full allocation. section 3.2

The padded Hello!

In CHAR(8), inserting Hello! uses 8 bytes because the two unused character positions are filled with whitespace. Address columns and fixed-length ZIP codes make the choice concrete. section 3.2

Listing 3.5 Updating the DepartmentCode column to a fixed-length string
ALTER TABLE dbo.Employees
   ALTER COLUMN DepartmentCode NCHAR(2) ;

Compare fixed-length alteration syntax with the DepartmentCode example. section 3.2

How 100 SQL Server Mistakes and How to Avoid Them covers it

How deep it goes
Traces whitespace padding into byte usage, 8 KB pages, and row-slot metadata.
Examples
Uses US address fields, CHAR(8) containing Hello!, and a DepartmentCode alteration.
What you can do after
Evaluate fixed-length string choices by accounting for padding and declared storage length.
How it connects
Connects CHAR with VARCHAR, NCHAR, SQL Server pages, extents, and row storage.

Also explained in

Compare how 5 books explain char(n) →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 SQL Server Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.