// dialect comparison

CHAR versus VARCHAR semantics in SQL Server, PostgreSQL, MySQL, SQLite and Oracle

Blank-padding is invisible until a join compares CHAR to VARCHAR and stops matching.

Filed under Types & casting

The verdict

CHAR buys nothing in modern engines except fixed-width semantics you almost never want; TEXT/VARCHAR2/VARCHAR is the default everywhere. If you inherit CHAR columns, test comparisons against VARCHAR values: trailing-space rules differ per engine and, in MySQL, per collation.

Where they differ

CHAR(n) pads short values with spaces; VARCHAR does not. What happens to those spaces on read and in comparisons is where the dialects quietly disagree, and SQLite never pads at all.

How each engine writes it

SQL Server

CREATE TABLE t (c CHAR(10), v VARCHAR(10));

CHAR pads on storage; trailing spaces are ignored in equality ('a' = 'a ' is true), and ANSI_PADDING is effectively always ON in modern versions.

PostgreSQL

CREATE TABLE t (c CHAR(10), v VARCHAR(10));

CHAR(n) (bpchar) stores and displays the padding, but treats trailing spaces as insignificant: they vanish when the value is cast to text or concatenated, and comparisons ignore them. TEXT or VARCHAR is the idiomatic choice; CHAR is not faster.

MySQL

CREATE TABLE t (c CHAR(10), v VARCHAR(10));

CHAR pads on disk and strips trailing spaces on retrieval. VARCHAR keeps them, and whether 'a ' = 'a' depends on the collation: MySQL 8's default utf8mb4_0900_ai_ci is NO PAD, so they differ, while PAD SPACE collations such as utf8mb4_general_ci ignore trailing spaces.

SQLite

CREATE TABLE t (c TEXT, v TEXT);

CHAR(n)/VARCHAR(n) are just TEXT affinity: no padding, no stripping, declared lengths ignored. Any length rule lives in a CHECK constraint.

Oracle

CREATE TABLE t (c CHAR(10), v VARCHAR2(10));

CHAR(10) pads. CHAR against CHAR or a literal uses blank-padded comparison, so c = 'a ' is true, but CHAR against a VARCHAR2 column does not pad, so c = v is false when both hold 'a'. Use VARCHAR2 for real strings.

Questions that come up

When is CHAR actually the right choice?

Rarely. Fixed-length codes such as a two-letter country code are the textbook case, and even there VARCHAR(2) stores the same data without the padding rules.

Why does 'a' = 'a ' return true on some engines?

SQL Server and PostgreSQL ignore trailing spaces when comparing character values. Oracle does when both sides are CHAR or literals, but not CHAR against VARCHAR2. MySQL does only under PAD SPACE collations, and SQLite never does (the default BINARY collation compares bytes).

Does CHAR(n) reject longer values?

Overlength values error (or truncate, depending on mode) just as with VARCHAR. Shorter values are padded to n on storage, so SQL Server, PostgreSQL and Oracle return 'a' plus nine spaces from a CHAR(10) column; MySQL strips them on the way out.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.