MySQL CHAR: Fixed-Length Strings and Examples
Learn how MySQL CHAR(M) counts characters, pads shorter values, handles trailing spaces, and treats overlength input in strict mode.
MySQL CHAR(M) stores fixed-length strings. M is the maximum number of characters, from 0 to 255; when omitted, it defaults to 1. Unlike VARCHAR(M), CHAR values are padded with spaces to the declared length when stored. MySQL removes trailing pad spaces when values are retrieved by default.
Syntax
CHAR(M)
M counts characters. The character set affects how many bytes those characters use.
Padding and trailing spaces
CREATE TABLE char_demo (
name CHAR(5) NOT NULL
);
INSERT INTO char_demo (name)
VALUES ('Tim');
SELECT name, CHAR_LENGTH(name), LENGTH(name)
FROM char_demo;
The value is padded to the declared width for storage. When selected, trailing spaces are removed by default, so CHAR_LENGTH() and LENGTH() report the retrieved string rather than the padded width. The PAD_CHAR_TO_FULL_LENGTH SQL mode changes that retrieval behavior.
Values longer than the declared length
CHAR(5) cannot store six non-space characters such as 'Steven' without truncation:
CREATE TABLE names (
name CHAR(5) NOT NULL
);
INSERT INTO names (name)
VALUES ('Steven');
With strict SQL mode enabled, MySQL rejects the value with Error 1406. Without strict mode, MySQL truncates an overlength value and reports a warning. See MySQL Error 1406: Data Too Long for Column for diagnosis and safe ways to handle overlength input.
For variable-length strings, character-set storage, and VARCHAR truncation rules, see MySQL VARCHAR and the MySQL 8.4 CHAR and VARCHAR reference.