Learn how SQL CHAR stores every value at a set width, padding shorter strings with spaces. See how fixed-width codes and identifiers differ from variable-length VARCHAR fields, and why the right type can make stored data more consistent and easier to handle.

Multiple Choice

Which data type stores fixed-length character values?

Fixed-length character storage is what CHAR provides. When you define a column as CHAR with a specified length, every value occupies that exact amount of space. If you store a shorter string, it’s padded to the defined length with spaces, ensuring every entry has the same width. This uniform size is useful when you know all values will be the same length, such as fixed codes or identifiers. That’s why CHAR is the best answer here: it is designed for fixed-length character data. The other types don’t enforce a constant width. VARCHAR stores variable-length strings, using only as much space as the value contains. Binary is used for binary data rather than text, and Date is for date values, not character strings.

CHAR and VARCHAR: How SQL Stores Text

A postal code, a two-letter region code, and a person’s full name are all text—but they don’t behave alike in a database. Some values have a set length. Others may be short or long. SQL has different character types for these needs, and CHAR is the type designed for fixed-length text.

That sounds simple, but the details matter. A column’s data type can affect how values are stored, compared, and returned. Once you understand the difference between fixed-width and variable-width text, choosing between CHAR and VARCHAR becomes much easier.

What fixed-length means in SQL

When you define a column as CHAR(n), you give it a set character length. For example:


region_code CHAR(2)

This column is intended to hold values with two characters, such as CA or NY. The width stays the same for every value. If a shorter string is stored, SQL commonly pads it with spaces on the right to reach the declared length.

For instance, if a column is defined as CHAR(5) and receives CAT, it may be stored as CAT —the three letters followed by two spaces. The value still looks like CAT when displayed in many database tools, because those tools may hide trailing spaces. The underlying behavior, though, comes from the fixed-width definition.

The idea is a bit like writing entries on a form with boxes that are all the same size. Every entry gets the same-width space, even if some need less room than others.

A fixed width for values that really are fixed

CHAR makes sense when the data has a consistent length by design. Think of a country code with two letters, a short status code, or a product category code that always uses the same number of characters.


CREATE TABLE locations (

country_code CHAR(2),

region_code  CHAR(3)

);

Here, country_code might hold US, while region_code might hold a three-character code. The data model says that each value has a known width, so fixed-width storage can be a natural fit.

This can also make a table’s structure easier to reason about. If every code follows the same length rule, that rule is visible in the column definition. It’s not a guarantee that every inserted value is meaningful, of course. CHAR(2) can restrict the width, but it won’t tell you whether ZZ is a valid country code. Other constraints or application rules may be needed for that.

How VARCHAR differs

VARCHAR(n) stores text that can vary in length, up to a maximum of n characters. A VARCHAR(50) column can contain a short value like Mia or a longer one like North Harbor.


display_name VARCHAR(50)

Unlike CHAR, VARCHAR doesn’t pad every shorter value to the full declared width. If the stored name has three characters, the database generally stores those characters plus a small amount of information about the value’s length. Exact storage details vary by database system.

That makes VARCHAR a common choice for text whose length isn’t known in advance. Names, descriptions, email addresses, and street addresses often vary. A fixed-width column for those values could leave a lot of padding behind.

There’s a small twist: the number in VARCHAR(50) is a limit, not a promise that each value will contain 50 characters. That’s the key contrast. CHAR(50) defines a fixed width; VARCHAR(50) allows values of different lengths up to the limit.

Why padding and comparison can matter

Trailing spaces may affect how a database handles a value, but behavior can differ across systems and data types. Some SQL systems ignore trailing spaces during certain character comparisons; others may handle them differently depending on the type, collation, or operation. So a value that appears to be CAT in a query result might still bring space-related surprises in a comparison or when data moves between systems.

That’s one reason to be cautious when mixing fixed- and variable-width columns. For example, importing a CHAR field into another system may preserve padding, trim it, or display it in a different way. If a value is used as an identifier, it’s worth checking how the database treats spaces rather than assuming every tool behaves the same.

When you inspect data, functions such as TRIM can remove leading or trailing spaces. But trimming should be deliberate. Spaces can be meaningful in some datasets, and removing them without checking may alter the original value.

What about binary and date data?

Not every column that holds information should be treated as text. Binary types store bytes, which are useful for data such as encoded files or raw values. A binary value isn’t the same thing as a word or code, even if it can be displayed as a sequence of symbols.

Date types serve a different purpose again. A date column stores a date value that SQL can sort and compare as a date. Storing dates as character strings may seem convenient, but it can lead to awkward sorting or inconsistent formats. For example, text dates written in different styles may not sort in calendar order.

Choosing a data type is about representing the meaning of the data, not just finding a container that accepts it. Character codes belong in character columns; dates belong in date columns; byte-oriented data belongs in binary columns.

Choosing CHAR or VARCHAR

A practical rule of thumb is to use CHAR when values are genuinely uniform in length, and VARCHAR when their lengths naturally vary. But don’t treat that as an absolute rule. Database systems differ in their storage details and performance characteristics, and a column’s role matters too.

Ask a few plain questions:

  • Do valid values always have the same number of characters?

  • Would trailing spaces be confusing or risky?

  • Is the value a code, or is it ordinary text?

  • What rules does the database use for comparisons and padding?

For a fixed two-letter region code, CHAR(2) may be a clear fit. For a person’s name, VARCHAR is usually more natural. A label such as ACTIVE or INACTIVE may have values of different lengths, so a variable-width type often suits it better—unless a particular system or data standard calls for a fixed-width field.

Also remember that CHAR and VARCHAR describe character data, not necessarily every detail of how characters are counted or encoded. A “character” can involve more than one byte, especially with Unicode text. The database’s character set and rules determine those details, so consult the documentation for the system you use.

The useful distinction

CHAR stores fixed-length character values. When a value is shorter than the defined width, it is commonly padded with spaces. VARCHAR stores character values whose lengths can vary up to a specified limit.

That distinction is small but useful. A fixed code can benefit from a fixed-width definition, while names and descriptions usually need room to grow—or shrink—without padding. Choose the type that reflects what the data means, and the database will be easier to understand later.