The ‘String or binary data would be truncated’ error will occur if an insert or update statement is trying to put too many characters into a field, defined in a table, which has been assigned too few character spaces. For example trying to write an email address with 255 characters into a table where the column email has been assigned 40 characters.
The easy fix is assign more characters to the column or columns you have determined are experiencing the problem. The more complicated but potentially necessary fix might be to change the logic or introduce validation at the source of data entry.
Finding the columns experiencing the problems however can be time consuming.
( . . . without the little script below of course)
SQL Server will kindly direct you to the stored procedure or insert/update statement that is experiencing the problem. However it will not pin point the exact column or columns that cannot be written to. The pain then is determining where the data won’t fit.
To speed things up take the entire query or query section you know to be causing the problem and write the results it into a temp table called #temp, i.e. SELECT * INTO #temp FROM SomeTable
Once the data has been written to the temp table #temp run the scrip below in the same window.
DECLARE @sql VARCHAR(MAX) SET @sql = ( SELECT ( SELECT ',MAX(LEN(' + NAME + ')) AS [' + NAME + ']' FROM tempdb.sys.columns WHERE object_id = object_id('tempdb..#temp') FOR XML PATH('') ) ) SET @sql = 'SELECT ' + RIGHT(@sql, LEN(@sql) - 1) + ' FROM #temp' EXEC (@sql)