Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String or binary data would be truncated, field find

I understand the error and how to fix I am just interested in finding the field to fix. Let me start from the top. I am running a scheduled task daily which executes a process that at some points runs some sprocs in sql which run insert statements. Unfortunately after checking my logs I am getting the error in question and therefore my sprocs arent working. I could update every field to a bigger length and this would probably fix it but id rather not. Is there any way of knowing (without manually checking as there are many fields and thousands of rows) the field that contains the value that is too big for the field it is being inserted into?

like image 314
Srb1313711 Avatar asked Mar 12 '14 15:03

Srb1313711


People also ask

Which field string or binary data would be truncated?

What is “String or binary data would be truncated” One of the most common SQL Server errors, the message “String or binary data would be truncated” occurs when a value is trying to be inserted or updated in a table and it is larger than the maximum field size.

How do you find the string or binary data would be truncated?

How to fix “String or binary data would be truncated” The main reason behind this error is the more amount of data that we are trying to store in a column than a specific column can store. So a quick solution to solve this error is by increase the column size.

How do I fix string or binary data would be truncated in SQL?

Solution. To avoid this error and to insert the string with truncation, use the ANSI_WARNINGS option. On setting ANSI_WARNINGS to OFF, the error message will not be displayed and the data will be automatically truncated to the length of the destination column and inserted.

Why string or binary data would be truncated The statement has been terminated?

"String or binary data would be truncated." The "String or binary data would be truncated" error occurs when the value persisted in a field is higher (in character count) than the one the database column max value allows.


1 Answers

Import the data into a new table using VARCHAR(MAX) as the datatype for the columns. Then you can use DATALENGTH to get the maximum size of each column.

SELECT MAX(DATALENGTH(col1)) AS col1, MAX(DATALENGTH(col2)) AS col2, etc.
  FROM newTable

This will tell you which column(s) exceed the size of your column(s).

like image 154
Neil Knight Avatar answered Sep 20 '22 13:09

Neil Knight