Getting the "value too long for type character varying(N)" error in SQL? This guide explains what causes this error and how to fix it with practical examples.
What Causes This Error?
The string truncation error (22001) occurs when:
- Inserting string longer than column allows
- Column defined with insufficient length
- Data from external source exceeds expectations
How to Fix It
- Increase column size: ALTER TABLE t ALTER COLUMN c TYPE VARCHAR(new_size).
- Truncate input in application before inserting.
- Use TEXT type for unlimited length (when appropriate).
- Validate input length before database operation.
Example: Wrong vs Correct
❌ Code That Causes the Error
-- Column defined as VARCHAR(50) INSERT INTO users (bio) VALUES ('This is a very long biography that exceeds the fifty character limit defined for this column'); -- Error: value too long
✅ Corrected Code
-- Either increase column size: ALTER TABLE users ALTER COLUMN bio TYPE VARCHAR(500); -- Or truncate input: INSERT INTO users (bio) VALUES (LEFT('Long text...', 50));
Quick Checklist
- [ ] Verify column/table names are spelled correctly
- [ ] Check data types match expected values
- [ ] Review query syntax for missing keywords
- [ ] Ensure referenced tables/columns exist
Related SQL Errors
If you're troubleshooting SQL errors, you might also encounter: