CHAR and VARCHAR Data Types

This section details the usage and characteristics of the CHAR and VARCHAR data types in SQL Server.

Note: These data types are fundamental for storing character data in SQL Server. Understanding their differences is crucial for efficient database design and performance.

CHAR Data Type

The CHAR(n) data type stores fixed-length character strings. When you declare a column as CHAR(n), each value stored in that column will always occupy exactly n bytes. If the string you store is shorter than n, it will be padded with spaces on the right to reach the specified length.

Example Usage:

CREATE TABLE ProductCodes (
    Code CHAR(5) PRIMARY KEY,
    Description VARCHAR(100)
);

INSERT INTO ProductCodes (Code, Description) VALUES ('ABC', 'Standard Widget');
-- 'ABC' will be stored as 'ABC  ' (padded with spaces)
INSERT INTO ProductCodes (Code, Description) VALUES ('XYZ12', 'Premium Gadget');
-- This will fail if 'XYZ12' is longer than 5 characters.

VARCHAR Data Type

The VARCHAR(n) data type stores variable-length character strings. The amount of storage space used is determined by the actual length of the string you store, plus two bytes to store the length of the string. This makes VARCHAR more space-efficient for data where the length varies significantly.

Example Usage:

CREATE TABLE CustomerNames (
    CustomerID INT PRIMARY KEY IDENTITY,
    FirstName VARCHAR(50),
    LastName VARCHAR(50)
);

INSERT INTO CustomerNames (FirstName, LastName) VALUES ('Alice', 'Smith');
-- Stores 'Alice' and 'Smith' exactly as provided.
INSERT INTO CustomerNames (FirstName, LastName) VALUES ('Bob', 'Johnson');
-- Stores 'Bob' and 'Johnson' exactly as provided.

Key Differences Summary

Feature CHAR(n) VARCHAR(n)
Length Fixed Variable
Storage Space n bytes + overhead Actual string length + 2 bytes overhead
Padding Pads with spaces to n bytes Does not pad trailing spaces
Use Case Fixed-format data (e.g., country codes, status flags) General text data where length varies (e.g., names, descriptions)

Performance Considerations

Tip: Always consider the expected range of data lengths. If lengths are consistently the same, CHAR might be suitable. Otherwise, VARCHAR is usually the better choice.

For Unicode character data, consider using NCHAR(n) and NVARCHAR(n) respectively, which use two bytes per character.