liveBook · Manning

Varchar data type

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

VARCHAR is a SQL Server variable-length, non-Unicode character data type that stores values using an 8-bit code page unless a UTF-8 collation is applied.

VARCHAR stores variable-length, non-Unicode character data in SQL Server, using an 8-bit code page unless a UTF-8 collation is applied. section 3.2

Mistake #7: assuming VARCHAR is always the most space-efficient string type. A VARCHAR(10) and CHAR(10) can both hold ten-character values, but fixed-length strings pad unused characters with spaces, while rows also carry storage metadata. section 3.2

US address columns

An address table might define CityName as VARCHAR(17), because Mooselookmeguntic and Kleinfeltersville have 17-character names, StateName as VARCHAR(48), and ZIP code as VARCHAR(10). For a consistently ten-character value, compare VARCHAR(10) with CHAR(10) instead of assuming the two choices consume identical storage. 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 a fixed-length NCHAR column definition with VARCHAR when reviewing schema choices. section 3.2

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

How deep it goes
Explains padding, pages, extents, row slots, and storage metadata, but stops short of a complete storage-cost calculation.
Examples
Uses US address fields, maximum place-name lengths, ZIP codes, and a DepartmentCode alteration.
What you can do after
Evaluate string-column definitions against value length, padding behavior, and SQL Server storage rather than assuming VARCHAR is always smaller.
How it connects
Connects VARCHAR with CHAR, NCHAR, Unicode handling, collations, and SQL Server page storage.

Also explained in

Compare how 4 books explain varchar data type →

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.