Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find "String or binary data would be truncated" error on sql in a big query

Tags:

sql

sql-server

I have a huge INSERT INTO TABLE1 (....) SELECT .... FROM TABLE2 statement. It gives me the error

"String or binary data would be truncated".

I know that one of the columns from TABLE2 is way bigger for one column from TABLE1 in the INSERT statement.

I have more than 100 columns in each table. So it is hard to find out the problem. Is there any easier way to figure this out?

like image 981
Arif YILMAZ Avatar asked Jun 11 '15 10:06

Arif YILMAZ


People also ask

How will you find string or binary data would be truncated in SQL?

To fix this error, patch to SQL Server 2016 SP2, CU6 or newer (including SQL Server 2017), and then turn on trace flag 460. You can enable it at the query level or at the server level.

How do you know if string or binary data will be truncated?

The "String or binary data would be truncated" error indicates that the procedure is attempting to store something in the DBServerInfo table that is larger than the column allows. The two known reasons this can occur are: SQL Server has at least one database whose name exceeds 25 characters in length.

How solve string or binary data would be truncated in a table?

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.

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

You can query Information_Schema.Columns for both tables and check the difference in content length.

Assuming your tables have the same column names, you can use this:

SELECT t1.Table_Name, t1.Column_Name FROM INFORMATION_SCHEMA.Columns t1 INNER JOIN INFORMATION_SCHEMA.Columns t2 ON (t1.Column_Name = t2.Column_Name) WHERE t1.Table_Name = 'Table1' AND  t2.Table_Name = 'Table2' AND ISNULL(t1.Character_maximum_length, 0) < ISNULL(t2.Character_maximum_length, 0) 

Assuming your tables have different column names, you can do this and just look for the difference

SELECT Table_Name, Column_Name, Character_maximum_length FROM INFORMATION_SCHEMA.Columns WHERE Table_Name IN('Table1', 'Table2') ORDER BY Column_Name, Character_maximum_length, Table_Name 
like image 144
Zohar Peled Avatar answered Sep 25 '22 18:09

Zohar Peled