Why it happens: the text does not fit in VARCHAR
CREATE TABLE codes_demo (
code VARCHAR(5)
);
INSERT INTO codes_demo (code)
VALUES ('ABC123');ABC123 is six characters long. On engines that enforce that limit, the row does not fit. Do not conclude that «the statement is badly written»: the syntax may be valid, and the conflict lies between the data and the schema.
How to fix it in SQL
- If the data is wrong, fix its source before the
INSERTorUPDATE. - If the data is valid and the limit is too short, widen the column using your engine's syntax and review indexes, dependencies and the cost of the change.
- If the limit represents a real rule, keep the constraint and validate the value before storing it.
- If you decide to truncate, do it only as an explicit business rule; silently truncating can lose important identifiers, names or descriptions.
Do not automatically switch every column to an «unlimited» type. A maximum can express a useful rule, and changing types on a large table can have implications for locking, storage or indexes.
How to check it
Measure the real value with the appropriate length function, and test multibyte characters.
How to avoid it next time
Truncating just to make the INSERT pass can destroy valid information.
Related cases worth checking
The same definition does not mean the same thing on every engine
| Engine | Relevant behaviour |
|---|---|
| PostgreSQL | varchar(n) limits it to n characters; a longer value raises an error, except for specific documented exceptions for trailing spaces or explicit casts. |
| MySQL 8.4 | VARCHAR(n) declares a maximum number of characters. In strict mode, a value that exceeds it is rejected; without strict mode it may be truncated with a warning. |
| SQL Server | In varchar(n), n expresses bytes, not necessarily characters. SQL Server 2019+ can show error 2628 with the table, column and truncated value when detailed warnings are enabled. |
| SQLite | The number of VARCHAR(n) is ignored: VARCHAR(5) has TEXT affinity and does not itself impose a five-character limit. |
Do not test the fix only on SQLite if production uses another engine. A row that SQLite accepts can be rejected by a column VARCHAR(n) on PostgreSQL, MySQL or SQL Server.
Diagnose the data before changing the schema
Start by comparing the real length of the value with the expected limit. This example is deliberately local and was checked in SQLite; it models a limit expressed in characters:
WITH input(field, value, max_characters) AS (
VALUES ('code', 'ABC123', 5),
('name', 'Cup', 40)
)
SELECT
field,
value,
length(value) AS characters,
max_characters,
CASE
WHEN length(value) > max_characters THEN 'exceeds'
ELSE 'ok'
END AS status
FROM input
ORDER BY field;| field | value | characters | max_characters | status |
|---|---|---|---|---|
| code | ABC123 | 6 | 5 | exceeds |
| name | Mug | 4 | 40 | ok |
The length function differs by engine: PostgreSQL and MySQL offer CHAR_LENGTH, SQLite uses length and SQL Server has LEN. In SQL Server, if the limit you are investigating is varchar(n), also check the bytes with DATALENGTH, especially with multibyte encodings.