Engine by engine
CHAR(n) pads to n and every engine cleans up the padding differently; SQLite ignores the whole idea and Oracle counts bytes.
| 引擎 | 類型 | 儲存 | 備註 |
|---|---|---|---|
| SQL Server |
CHAR(n)
|
n bytes, 1-8000 | Blank-padded on storage; = and LIKE both ignore the stored padding, but trailing spaces inside a LIKE pattern count. |
| PostgreSQL |
CHAR(n) (character(n))
|
n chars + small overhead | Padded to n characters; casts and most comparisons strip the padding. |
| MySQL |
CHAR(n)
|
n chars x charset bytes | utf8mb4 reserves up to 4 bytes per character; retrieval strips trailing spaces (VARCHAR keeps them). |
| SQLite |
CHAR(n) (TEXT affinity)
|
variable | No padding and no length enforcement: anything non-TEXT is coerced and n is ignored. |
| Oracle |
CHAR(n) / NCHAR(n)
|
n bytes or n chars, max 2000 | BYTE semantics by default; CHAR semantics via CHAR(n CHAR) or NLS_LENGTH_SEMANTICS. |
Gotchas
Where the engines disagree. These tend to surface as bugs during a migration or a port.
- CHAR(n) pads values shorter than n, and engines disagree on cleanup: MySQL strips padding on retrieval, PostgreSQL strips it on cast to text, SQL Server and Oracle return it padded and ignore it in = comparisons.
- SQLite accepts CHAR(n) as TEXT affinity and never enforces n or pads - a schema port that silently changes semantics.
- Oracle sizes CHAR in bytes by default: in AL32UTF8 a CJK character takes 3 bytes, so CHAR(10) rejects a 4-character Japanese value. Declare CHAR(n CHAR) instead.
- VARCHAR(n) is almost always the better default today; CHAR earns its keep only for truly fixed codes like country and currency.
Common questions
Does CHAR save space over VARCHAR?
Rarely. Variable-length storage costs only a 1-2 byte length prefix, and with multi-byte character sets CHAR is not even fixed-size on disk. The PostgreSQL docs call character(n) the slowest of its three string types.
Why do trailing spaces disappear from CHAR columns?
Because every engine handles the padding differently: MySQL strips it on retrieval, PostgreSQL strips it on cast to text, and SQL Server and Oracle keep it but ignore it in = comparisons.
Related on SQLly
- SQL data type comparison and converter Free tool
- CHAR versus VARCHAR semantics in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Cast syntax in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Convert a number to formatted text in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- SQL CHR / CHAR function 函式
- SQL cast to text 函式
Map a type to the other four engines
The free type converter maps a type from one engine to its closest equivalent in the others and says where storage, precision or behaviour differ. The desktop app does the same mapping while you edit.