SQL error · INSERT, UPDATE, DELETE and schema

The text does not fit in VARCHAR

Problem: The value exceeds the defined limit, or the unit of measurement the engine uses.

Solution: Fix the data if it is invalid; widen the column through a migration if the value is legitimate.

Why it happens: the text does not fit in VARCHAR

Conceptual example
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.

“Text does not fit in VARCHAR” error messages depending on the engine

The exact wording depends on the database. Compare these common messages with the one in your console.

EngineTypical message
PostgreSQLERROR: value too long for type character varying(5)
MySQLERROR 1406 (22001): Data too long for column 'code' at row 1
SQL ServerMsg 2628, Level 16: String or binary data would be truncated in table 'store.dbo.codes_demo', column 'code'. Truncated value: 'ABC12'. (Msg 8152 en versions previous)
OracleORA-12899: value too large for column "ESQUEMA"."CODIGOS_DEMO"."CODIGO" (current: 6, maximum: 5)

SQLite does not enforce the VARCHAR(n) limit: it stores the full text without an error.

How to fix it in SQL

  1. If the data is wrong, fix its source before the INSERT or UPDATE.
  2. 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.
  3. If the limit represents a real rule, keep the constraint and validate the value before storing it.
  4. 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

EngineRelevant behaviour
PostgreSQLvarchar(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.4VARCHAR(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 ServerIn 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.
SQLiteThe 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:

Compare length and limit
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;
Result verified in SQLite
fieldvaluecharactersmax_charactersstatus
codeABC12365exceeds
nameMug440ok

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.