For years, SQL Server's most frustrating error was this:
Msg 8152, Level 16, State 30
String or binary data would be truncated.
The statement has been terminated.
Which table? Which column? Which row? It didn't say. On a 60-column insert from an ETL job, finding it meant bisecting the data by hand. The good news is that modern versions tell you exactly where the problem is.
What it means
You're putting a value into a column that's too small for it:
- a 300-character string into
varchar(255); nvarchardata into a shorternvarchar(n);ncounts characters, but the byte limits are different forvarcharwith UTF-8 collations;- a
varbinaryvalue that's too long; - an implicit conversion that makes a value longer than you expect (for example a number or date converted to a string column).
SQL Server stops the statement rather than silently cutting the data (unless ANSI_WARNINGS is off; see below).
Get the detailed message (2628)
SQL Server 2019 and later replace 8152 with message 2628, which names the table, column and value:
Msg 2628, Level 16, State 1
String or binary data would be truncated in table 'Shop.dbo.Customers',
column 'Email'. Truncated value: 'averyveryveryverylongaddress@exam'.
It's on by default for databases at compatibility level 150+. If you still see 8152 on 2019+, check:
SELECT name, compatibility_level FROM sys.databases WHERE name = DB_NAME();
-- Database-scoped switch (ON by default at level 150+)
ALTER DATABASE SCOPED CONFIGURATION SET VERBOSE_TRUNCATION_WARNINGS = ON;
On SQL Server 2016 SP2 CU6+ and 2017 CU12+, you can get the same detailed message with trace flag 460:
