SQL Error Guide

SQL Error: String Truncation (Fix + Examples)

Fix the SQL "value too long for type character varying(N)" error. Learn what causes it and see examples of how to resolve it.

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

  1. Increase column size: ALTER TABLE t ALTER COLUMN c TYPE VARCHAR(new_size).
  2. Truncate input in application before inserting.
  3. Use TEXT type for unlimited length (when appropriate).
  4. 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:

Related Content

From Our Blog

Still stuck?

Practice SQL in our sandbox environment with instant error feedback.