"String or binary data would be truncated" (Msg 2628, 8152)

String or binary data would be truncated: SQL Server error 2628 or 8152 means a value is longer than its column. How to find the column, and the fixes.

What “string or binary data would be truncated” means

SQL Server raises this error when a statement tries to put a value into a column that is too short for it. Take a small hotel database with guests, rooms and bookings, a cut-down copy of the hotel management system template. A guest’s phone number is varchar(15):

CREATE TABLE dbo.Guest (
  GuestId  int NOT NULL CONSTRAINT PK_Guest PRIMARY KEY,
  FullName nvarchar(100) NOT NULL,
  Phone    varchar(15) NULL
);
CREATE TABLE dbo.Room (
  RoomId     int NOT NULL CONSTRAINT PK_Room PRIMARY KEY,
  RoomNumber varchar(10) NOT NULL
    CONSTRAINT AK_Room_RoomNumber UNIQUE
);
CREATE TABLE dbo.Booking (
  BookingId int  NOT NULL CONSTRAINT PK_Booking PRIMARY KEY,
  GuestId   int  NOT NULL,
  RoomId    int  NOT NULL,
  CheckInOn date NOT NULL,
  CONSTRAINT FK_Booking_Guest_GuestId FOREIGN KEY (GuestId)
    REFERENCES dbo.Guest (GuestId),
  CONSTRAINT FK_Booking_Room_RoomId FOREIGN KEY (RoomId)
    REFERENCES dbo.Room (RoomId)
);
dbo.Guest with primary key GuestId, FullName and Phone; dbo.Room with primary key RoomId and alternate key RoomNumber; dbo.Booking with foreign keys GuestId and RoomId; lines from Guest and from Room to Booking.
Three tables of the hotel. Phone is varchar(15), RoomNumber varchar(10).

A guest from London writes the number with spaces, as people do:

INSERT INTO dbo.Guest (GuestId, FullName, Phone)
VALUES (1, N'Ana Lima', '+44 20 7946 0958');
Msg 2628, Level 16, State 1, Line 1
String or binary data would be truncated in table 'Hotel.dbo.Guest', column 'Phone'. Truncated value: '+44 20 7946 095'.
The statement has been terminated.

The value is 16 characters long and the column holds 15. SQL Server won’t drop the last character and store the rest, so it refuses the statement and no row is written. The error ends that one statement. The batch goes on, so whatever comes after the failed INSERT still runs, and a script that carries on as if the guest exists will fail again further down.

The truncated value in the message is the value cut to the column’s length, the part that would have fit, not the full text you sent. Here it is the number without its last digit. That cut-off start is usually enough to find the row in the source.

Which statements raise it

Every statement that writes a character or binary value into a column checks its length:

  • INSERT, with VALUES or with a SELECT;
  • UPDATE and MERGE that set a column to a longer value (the same Msg 2628, for the same phone number);
  • ALTER TABLE ... ALTER COLUMN that makes a column shorter than a value it already holds; the message then shows an empty truncated value, '';
  • binary and varbinary columns, the “binary” in the message. Their truncated value shows empty too.

Trailing spaces don’t count. A 15-character number followed by five spaces goes into varchar(15) without an error, and the stored value is 15 bytes. SET ANSI_WARNINGS documents this: trailing blanks are ignored for character columns.

Other places cut a value with no error at all. A variable, a stored procedure’s parameter, and CAST or CONVERT to a shorter type all truncate silently. Here the variable is shorter than the column:

DECLARE @Phone varchar(12) = '+1 415 555 0134';
INSERT INTO dbo.Guest (GuestId, FullName, Phone)
VALUES (3, N'Ben Okafor', @Phone);
SELECT Phone FROM dbo.Guest WHERE GuestId = 3;

The INSERT succeeds and stores +1 415 555 0. The number was cut when it went into the 12-character variable, so the column never saw the long value. The same page says ANSI_WARNINGS isn’t honored for procedure and function parameters or for variables, which is why procedure parameters deserve the same sizes as the columns they fill.

Msg 2628, Msg 8152 and how to turn on the detailed message

The same INSERT on a database that runs the older message gives you this:

Msg 8152, Level 16, State 30, Line 1
String or binary data would be truncated.
The statement has been terminated.

Msg 8152 names no table, no column and no value. Which one you get depends on the version and the database’s compatibility level, per Microsoft’s docs for ALTER DATABASE SCOPED CONFIGURATION and trace flag 460:

  • SQL Server 2019 and later, at compatibility level 150 or higher: Msg 2628 by default. The database setting VERBOSE_TRUNCATION_WARNINGS chooses, and it is ON unless someone turned it off. Set to OFF, it brings back Msg 8152, which is how the message above was made.
  • Compatibility level 140 or lower: Msg 2628 only with trace flag 460, which SQL Server 2016 SP2 CU6, 2017 CU12 and later versions have. The database setting has no effect there.

To turn it back on in a database at level 150 or higher:

ALTER DATABASE SCOPED CONFIGURATION
  SET VERBOSE_TRUNCATION_WARNINGS = ON;

To check the level, and to turn the flag on for your own connection when the level is 140 or lower (it needs the sysadmin role):

SELECT name, compatibility_level
FROM sys.databases
WHERE name = DB_NAME();

DBCC TRACEON (460);

With the Hotel database set to level 140, the insert raised Msg 8152; after DBCC TRACEON (460) the same connection got Msg 2628. Without the flag, find the column by hand: list the target’s column sizes and compare them with the lengths of the values you send. The section on copied values shows the query.

A column sized too small

The phone column is the plain case. It was sized for the numbers the designer had in mind, not for what guests type: country codes, spaces, an extension. The fix is to widen it. The hotel template uses varchar(20):

ALTER TABLE dbo.Guest
  ALTER COLUMN Phone varchar(20) NULL;
dbo.Guest with Phone now varchar(20), beside dbo.Room and dbo.Booking with their lines unchanged.
Phone is varchar(20) now. The London number fits, with room for a few more characters.

After it the London insert succeeds. Repeat NULL or NOT NULL in the ALTER COLUMN: left out, the column comes back nullable, even one that was NOT NULL. For nvarchar columns, what n counts is in nvarchar vs varchar.

Take the length from the data’s real limit (a standard, the field in the system that sends it), never from the sample rows you have. And don’t reach for SET ANSI_WARNINGS OFF: with it, the London insert succeeds and stores +44 20 7946 095, a wrong phone number and no error.

A child column narrower than its parent

Room numbers are varchar(10) with a unique constraint, so 204 and the penthouse’s PH-1201 both fit. Housekeeping logs issues by room number, in a table someone gave a varchar(5). With the foreign key declared, SQL Server refuses the table outright:

CREATE TABLE dbo.RoomIssue (
  RoomIssueId int NOT NULL
    CONSTRAINT PK_RoomIssue PRIMARY KEY,
  RoomNumber  varchar(5) NOT NULL,
  Note        nvarchar(400) NOT NULL,
  CONSTRAINT FK_RoomIssue_Room_RoomNumber
    FOREIGN KEY (RoomNumber)
    REFERENCES dbo.Room (RoomNumber)
);
Msg 1753, Level 16, State 1, Line 1
Column 'dbo.Room.RoomNumber' is not the same length or scale as referencing column 'RoomIssue.RoomNumber' in foreign key 'FK_RoomIssue_Room_RoomNumber'. Columns participating in a foreign key relationship must be defined with the same length and scale.
Msg 1750, Level 16, State 1, Line 1
Could not create constraint or index. See previous errors.

A declared foreign key must have its parent column’s type and length, as primary key vs foreign key shows for int and bigint. So the narrow child survives only where the foreign key was left out, and then it waits for the first long value:

CREATE TABLE dbo.RoomIssue (
  RoomIssueId int NOT NULL
    CONSTRAINT PK_RoomIssue PRIMARY KEY,
  RoomNumber  varchar(5) NOT NULL,
  Note        nvarchar(400) NOT NULL
);
dbo.Room with alternate key RoomNumber varchar(10); dbo.RoomIssue with a RoomNumber varchar(5) column of its own; no line between them, beside dbo.Guest and dbo.Booking.
No line: dbo.RoomIssue holds room numbers, but nothing ties its varchar(5) to the varchar(10) in dbo.Room.
INSERT INTO dbo.Room (RoomId, RoomNumber)
VALUES (1, '204'), (2, 'PH-1201');
INSERT INTO dbo.RoomIssue (RoomIssueId, RoomNumber, Note)
VALUES (1, '204', N'Shower drains slowly');
INSERT INTO dbo.RoomIssue (RoomIssueId, RoomNumber, Note)
VALUES (2, 'PH-1201', N'Balcony door sticks');
Msg 2628, Level 16, State 1, Line 5
String or binary data would be truncated in table 'Hotel.dbo.RoomIssue', column 'RoomNumber'. Truncated value: 'PH-12'.
The statement has been terminated.

PH-1201 is a valid room, it fits dbo.Room, and it can’t be logged. Give the child the parent’s length and declare the relationship:

ALTER TABLE dbo.RoomIssue
  ALTER COLUMN RoomNumber varchar(10) NOT NULL;
ALTER TABLE dbo.RoomIssue
  ADD CONSTRAINT FK_RoomIssue_Room_RoomNumber
    FOREIGN KEY (RoomNumber)
    REFERENCES dbo.Room (RoomNumber);
dbo.Room with alternate key RoomNumber varchar(10); dbo.RoomIssue with foreign key RoomNumber, now varchar(10); a line from Room to RoomIssue, beside dbo.Guest and dbo.Booking.
The line runs from the alternate key on RoomNumber, varchar(10) at both ends.

The penthouse issue goes in now. With the foreign key in place the two columns can’t drift apart again, because SQL Server won’t change one end alone: an ALTER COLUMN on dbo.Room.RoomNumber fails with Msg 5074, naming the foreign key that depends on the column. To widen room numbers later, drop the foreign key, alter both columns, and add it back.

A value copied into a shorter column

Invoices keep the name they were billed to, copied from the guest at checkout. FullName is nvarchar(100), BillTo only nvarchar(50):

CREATE TABLE dbo.Invoice (
  InvoiceId int NOT NULL CONSTRAINT PK_Invoice PRIMARY KEY,
  BookingId int NOT NULL,
  BillTo    nvarchar(50) NOT NULL,
  CONSTRAINT FK_Invoice_Booking_BookingId
    FOREIGN KEY (BookingId)
    REFERENCES dbo.Booking (BookingId)
);
dbo.Guest, dbo.Room and dbo.Booking as before; dbo.Invoice with foreign key BookingId and BillTo nvarchar(50); a line from Booking to Invoice.
BillTo is nvarchar(50), FullName nvarchar(100). No line joins the two columns: a copy is no relationship.
INSERT INTO dbo.Guest (GuestId, FullName)
VALUES (2,
  N'Maria Fernanda Albuquerque de Souza Vasconcelos Lima');
INSERT INTO dbo.Booking (BookingId, GuestId, RoomId, CheckInOn)
VALUES (1, 2, 1, '2026-06-12');
INSERT INTO dbo.Invoice (InvoiceId, BookingId, BillTo)
SELECT b.BookingId, b.BookingId, g.FullName
FROM dbo.Booking AS b
JOIN dbo.Guest AS g ON g.GuestId = b.GuestId;
Msg 2628, Level 16, State 1, Line 6
String or binary data would be truncated in table 'Hotel.dbo.Invoice', column 'BillTo'. Truncated value: 'Maria Fernanda Albuquerque de Souza Vasconcelos Li'.
The statement has been terminated.

One long name stops the whole INSERT ... SELECT. In a second run with a short name beside it, neither invoice was written. On a server that answers with Msg 8152, find the column and the row this way: the target’s sizes first, then the source rows longer than the size you suspect. LEN ignores trailing spaces, as the length check does:

SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'Invoice'
  AND CHARACTER_MAXIMUM_LENGTH IS NOT NULL;

SELECT g.GuestId, LEN(g.FullName) AS Characters
FROM dbo.Booking AS b
JOIN dbo.Guest AS g ON g.GuestId = b.GuestId
WHERE LEN(g.FullName) > 50;

It returns BillTo at 50 characters and guest 2 at 52. Either make the copy as wide as its source:

ALTER TABLE dbo.Invoice
  ALTER COLUMN BillTo nvarchar(100) NOT NULL;
dbo.Invoice with BillTo now nvarchar(100), beside dbo.Guest, dbo.Room and dbo.Booking; a line from Booking to Invoice.
BillTo matches FullName at nvarchar(100), so every name a guest can have fits on the invoice.

or, when the target really has a shorter limit, cut the value on purpose, so the decision is in the code and not left to an error:

INSERT INTO dbo.Invoice (InvoiceId, BookingId, BillTo)
SELECT b.BookingId, b.BookingId, LEFT(g.FullName, 50)
FROM dbo.Booking AS b
JOIN dbo.Guest AS g ON g.GuestId = b.GuestId;

How ERDsmith helps

All three failures were set at design time, in a column’s length. The rows only showed them. On a diagram the lengths sit next to each other before any data arrives:

  • Every column shows its type and length beside its name, so Phone varchar(15) and BillTo nvarchar(50) are in plain sight, one table away from FullName nvarchar(100).
  • dbo.RoomIssue gets its RoomNumber from the relationship to the unique key on dbo.Room.RoomNumber, with the parent column’s type and length: varchar(10), and it keeps following the parent when the parent changes.
  • The child’s length can’t be set apart from the parent’s, so the foreign key SQL Server refuses with Msg 1753 never gets drawn: pasted into ERDsmith, the first dbo.RoomIssue script gives no line.

A copy between two columns that no relationship joins, like BillTo, isn’t checked: that comparison is yours, and the diagram puts both columns on one screen.