string data right truncation
SQLSTATE 01004 (warning class) is raised when a string value is silently truncated to fit the declared length of a VARCHAR(n) or CHAR(n) column. This is a warning-level notification; in some contexts Postgres raises the error-level 22001 instead.
- 1Inserting or updating a character column with a string longer than the column declaration allows
Inserting an oversized string into a VARCHAR(10) column in a context where truncation is permitted.
INSERT INTO products (sku) VALUES ('TOOLONGVALUE123');expected output
WARNING: string data right truncation
Fix 1
Truncate the input explicitly before insert
WHEN When the data source can produce oversized strings.
INSERT INTO products (sku) VALUES (LEFT('TOOLONGVALUE123', 10));Why this works
LEFT() enforces the length limit in SQL before the row is written, documenting intent clearly.
Fix 2
Widen the column if the data legitimately needs more space
WHEN When the column size was set too small for actual values.
ALTER TABLE products ALTER COLUMN sku TYPE VARCHAR(30);
Why this works
Resizing the column prevents truncation without data loss.
✕ Rely on silent truncation in production
Silent data loss can corrupt business-critical identifiers and codes.
Class 01 — Warning
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev