Home SQL Server Errors 2628
Microsoft SQL Server

SQL Server Error 2628

String or binary data would be truncated in table (named column)

Severity 16 Data Type & Conversion
Applies to: SQL Server 2019 and later, SQL Server 2017 with trace flag 460, Azure SQL Database, Azure SQL Managed Instance
The full message
Msg 2628, Level 16, State 1, Line 1
String or binary data would be truncated in table 'Shop.dbo.Customers', column 'name'. Truncated value: 'Alexandra Constantinople'.

Why it happens

Error 2628 means a value you are writing does not fit the column it is going into. The column has a fixed maximum length, such as VARCHAR(20), and the value is longer, so SQL Server refuses to silently chop it off and stops the statement instead. This is the same underlying cause as the older error 8152.

What makes 2628 so much easier to work with is the detail in the message. Where 8152 only told you that some data would be truncated, 2628 names the table (Shop.dbo.Customers), the exact column (name), and even the offending value (Alexandra Constantinople). You no longer have to guess which column or which row is at fault, you can fix it immediately.

This improved message appears only on newer configurations. You get 2628 when the database is at compatibility level 150 or higher (SQL Server 2019 and later), or on SQL Server 2017 when trace flag 460 is enabled. On older versions or lower compatibility levels the same write raises the vaguer 8152 instead. The error is severity 16, a normal catchable data error, not a server fault.

Examples

Inserting a value that is too long for a named column

CREATE TABLE dbo.Customers (
    id   INT PRIMARY KEY,
    name VARCHAR(20)
);

-- 'Alexandra Constantinople' is 24 characters, the column holds 20
INSERT INTO dbo.Customers (id, name)
VALUES (1, 'Alexandra Constantinople');
Result
Msg 2628, Level 16, State 1, Line 7
String or binary data would be truncated in table 'Shop.dbo.Customers', column 'name'. Truncated value: 'Alexandra Constantinople'.

An UPDATE that overflows the column

-- The name column is VARCHAR(20); the new value is longer.
UPDATE dbo.Customers
SET name = 'Alexandra Constantinople'
WHERE id = 1;
Result
Msg 2628, Level 16, State 1, Line 2
String or binary data would be truncated in table 'Shop.dbo.Customers', column 'name'. Truncated value: 'Alexandra Constantinople'.

The same write on an older configuration raises 8152

-- Under compatibility level 140 or lower, and without trace flag 460,
-- SQL Server gives the vaguer message with no table, column, or value.
INSERT INTO dbo.Customers (id, name)
VALUES (1, 'Alexandra Constantinople');
Result
Msg 8152, Level 16, State 30, Line 3
String or binary data would be truncated.

How to fix it

Widen the named column with ALTER TABLE

The message tells you exactly which column is too small. If the longer value is legitimate, widen the column to fit. Here the name column named in the error is enlarged from 20 to 100 characters.

ALTER TABLE dbo.Customers
ALTER COLUMN name VARCHAR(100);

-- The insert now succeeds
INSERT INTO dbo.Customers (id, name)
VALUES (1, 'Alexandra Constantinople');

Shorten or validate the value before writing

If the column width is correct and the value is simply too long, trim or reject it. Use LEFT to cut it to the column size, or validate the length in the application first so bad data never reaches the table.

-- Fit the value to the 20-character column
INSERT INTO dbo.Customers (id, name)
VALUES (1, LEFT('Alexandra Constantinople', 20));

-- Or check the length before inserting
IF LEN('Alexandra Constantinople') <= 20
    INSERT INTO dbo.Customers (id, name)
    VALUES (1, 'Alexandra Constantinople');

Enable the detailed message on SQL Server 2017

If you are on SQL Server 2017 and still see the unhelpful 8152, turn on trace flag 460 to get the 2628 message that names the table, column, and value. On SQL Server 2019 and later, raise the database compatibility level to 150 instead.

-- SQL Server 2017: enable the detailed truncation message
DBCC TRACEON(460, -1);

-- SQL Server 2019+: raise compatibility level to 150 or higher
ALTER DATABASE Shop SET COMPATIBILITY_LEVEL = 150;

How to prevent it

Size columns to the real data. Measure the longest legitimate value a column must hold and pick a width with a little headroom, rather than guessing small. For a full rundown of string and binary lengths see SQL data types explained.

Validate input length before it reaches the database. Enforce maximum lengths in the application and in form fields so over-long values are caught early, and keep your databases at compatibility level 150 or higher so any truncation that does slip through reports the clear 2628 message instead of the vaguer 8152.

Common questions

What is the difference between error 2628 and error 8152?

They report the same problem: a value too long for its column. Error 8152 is the old, vague message that only says data would be truncated. Error 2628 is the modern replacement that also names the table, the column, and the truncated value, so you can fix it immediately. You get 2628 at database compatibility level 150 or higher, or on SQL Server 2017 with trace flag 460.

How do I fix SQL Server error 2628?

The message names the column that is too small. Either widen it with ALTER TABLE ... ALTER COLUMN so the value fits, or shorten and validate the value before writing it. If the value is legitimate, widen the column; if it is bad data, reject or trim it.

Why do I see 8152 instead of 2628?

You are on an older configuration. The detailed 2628 message needs database compatibility level 150 or higher (SQL Server 2019 and later), or SQL Server 2017 with trace flag 460 enabled. On lower compatibility levels the same truncation raises the vaguer 8152 with no table, column, or value.

Still stuck on a SQL Server error?

Browse the full SQL Server error library, or get hands-on help from an experienced SQL professional.