Why would this value be truncated by SQL Server?

Why would this value be truncated by SQL Server?

I get the following error from EF Core:

Microsoft.Data.SqlClient.SqlException (0x80131904): String or binary data would be truncated in table 'xxx.dbo.CheckImageApiLog', column 'RoutingNumber'. Truncated value: '150199880'.

The column RoutingNumber is defined as varchar(9), and the value 150199880 is exactly 9 characters in length. So why would SQL Server be unable to save this value? Could there be any hidden characters that's not visible?

There are no triggers or any other restrictions on the table. It was a simple table to log some API results.

Answer

Even though the error shows Truncated value: '150199880', that is actually the first nine characters of your input string, not the full value you passed.

SQL Server’s “verbose truncation” feature (introduced in SQL 2016 SP2 CU6/2017 CU12 and on by default in 2019+) reports exactly what would fit in the column. Read more

So when you see a nine‐character truncated value in a varchar(9) column, the real input must have been longer than nine characters

Enjoyed this article?

Check out more content on our blog or follow us on social media.

Browse more articles