[Last Call] Learn how to a build a cloud-first strategyRegister Now


string or binary data would be truncated - WHICH FIELD?

Posted on 2012-08-20
Medium Priority
Last Modified: 2012-09-24
THis is a highly annoying SQL error, especially since i have a huge SQL statement in a table with 30 different columns.  how do I know which field is getting kicked out without having to dissect every element of my statement?
Question by:arthurh88
LVL 66

Expert Comment

by:Jim Horn
ID: 38313268
No clue, but the way I typicall troubleshoot it is to have the table design of the source table in one window, and the table design of the destination table in another window, and compare column-by-column.

> how do I know which field is getting kicked
truncated just means characters to the right are not being inserted/updated.
LVL 12

Expert Comment

ID: 38313285
So this is an SSIS package? Or an import wizard, which is the same anyway? Or are you using Bulk load?

And what is the error message in it's entirity?
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 38313348
try commenting out few column names from source and destinations and run the statement.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 66

Expert Comment

by:Jim Horn
ID: 38313540
<posting this just in case it's not clear>

So if your source has a column that is a varchar(10), and your destination column that you're trying to insert/update is a varchar(5), the warning message is saying that you are at risk of losing the five characters to the right, regardless of whether it's populated or not.

So, if your source varchar(10) contains 'abcdefg'
What will be inserted to the destination varchar(5) is 'abcde', with the warning message.
LVL 75

Expert Comment

by:Anthony Perkins
ID: 38313659
THis is a highly annoying SQL error, especially since i have a huge SQL statement in a table with 30 different columns.  how do I know which field is getting kicked out without having to dissect every element of my statement?
What do you propose?  Are you suggesting that SQL Server list all the columns that could be truncated?  If that is the case, then I am afraid for a number of reasons that is not going to happen.
LVL 18

Expert Comment

ID: 38313819
It would be nice if it told you the column but unfortunately, it doesn't.  You will have to do some manual investigation.  
At least as a takeaway, you can understand the importance of properly constraining the data in the source so it stays at or below the column limit in your database.
LVL 27

Expert Comment

ID: 38314561
The column(s) at risk are the ones that are longer than the target columns. How can you not know the sizes of your columns? And if you are generating values, are you defining their lengths when you generate them? If not, then there might not be a way to tell without creating a new query that generates the same values and shows their lengths.

LVL 70

Accepted Solution

Scott Pletcher earned 2000 total points
ID: 38316908
Yes, IT REALLY IS ANNOYING.  I too think SQL should specify the column(s) ... but, as we know, it doesn't.

If it's an INSERT (95+% of the time), what I typically do is create the structure in tempdb, but change every string/binary column to varchar|bin(max).

INSERT INTO tempdb.dbo.test_table
FROM ...

Then, after it's run, do a single query to get the MAX(LEN( of every column, and compare that to the max lengths of the original/output data.  That allows you to identify all over-length columns at the same time.

For example, something like this:

USE maindb

    CASE c.name WHEN 'columnA' THEN [columnA] WHEN 'columnB' THEN [columnB] ... END AS max_length,
    CASE WHEN c.max_length = -1 THEN 'ok'
        WHEN c.name = 'columnA' THEN [columnA]
        WHEN c.name = 'columnB' THEN [columnB]
    END > c.max_length THEN 'TOO LONG!' ELSE 'ok' END AS check_length,
        MAX(LEN(columnA)) AS columnA, MAX(LEN(columnB)) AS columnB,
        MAX(LEN(columnC)) AS columnC, --, ...
    FROM tempdb.dbo.test_table
) AS tt
CROSS JOIN maindb.sys.columns c ON
    c.object_id = OBJECT_ID('dbo.table_name')

Author Closing Comment

ID: 38430494
glad to know someone relates!!

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Suggested Courses

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question