SQLSTATE 22001 ERROR Class 22: Data Exception

string_data_right_truncation String Data Right Truncation — SQLSTATE 22001

A string value is too long for the target character column.

PG 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed May 2025 Grounded in source

Symptoms

A string value is too long for the target character column.

  • The error is written to the server log and returned to the client carrying SQLSTATE 22001.
  • Any driver (libpq, JDBC, psycopg, npgsql, pgx) surfaces this code in its error object so you can branch on it programmatically.
  • PL/pgSQL can trap it by name: EXCEPTION WHEN string_data_right_truncation THEN.

Environment

Severity: ERROR  |  PostgreSQL versions: 12, 13, 14, 15, 16, 17

Reproduce with the exact statement and read the full message in the server log (raise log_min_messages / set log_min_error_statement for more context).

Root Cause

The value exceeds the declared length of a varchar(n) or char(n) column or cast.

Common causes:

  • Inserting or updating text longer than the column width.
  • An explicit cast to a too-small varchar(n).
  • Multibyte characters making the string longer than expected.

Diagnostic Queries

Recovery

Steps to resolve 22001:

  1. Widen the column: ALTER TABLE t ALTER COLUMN c TYPE varchar(NNN);, or use text for no limit.
  2. Validate or trim input length in the application before writing.
  3. If truncation is acceptable, apply it explicitly with left(value, n).

Reference: PostgreSQL error codes — Class 22 (Data Exception).

Was this helpful?