SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Avoid Statement Termination due to possible string truncation.


Avoid Statement Termination due to possible string truncation.

Author
Message
funooni
funooni
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1064 Visits: 338
Hi,

"string or binary data would be truncated. the statement has been terminated"

This is a very common error about possible truncation of data when we try to insert something bigger than the column size. Let's say the column size is varchar(20) and we try to insert string of size 21 then we get this error.

My question is "Is there a way to tell SQL Server to please go ahead and truncate the data and don't terminate the query "?

I have a scenario where I would like it to truncate and insert only as much as possible. But i don't want to stop the query execution.

Many Thanks
Thom A
Thom A
SSC Guru
SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)

Group: General Forum Members
Points: 82225 Visits: 21073
Yes, you with the following
SET ANSI_WARNINGS OFF;

--INSERT operation

SET ANSI_WARNINGS ON;


It's important to remember to switch the warnings back on afterwards.

Personally, however, rather than allowing the truncation, consider using LEFT.


Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does :-P

Please always remember to encapsulate your code in IFCode Markup. For example [code=sql] [/code].
Click here to read Jeffs Guide on how to post SQL questions, and get swift and helpful answers from the community
funooni
funooni
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1064 Visits: 338
Thanks Thom,

But this would just disappear the warning. Let me make it more clear about what i am looking for



create table TEST_INSERT
(id int , name varchar (3))

GO


INSERT INTO TEST_INSERT
SELECT 1, 'FOUR'

In case the warnings are on i get the error, otherwise it says completed successfully and in both cases nothing gets inserted.
What I am looking for is to have the first 3 characters inserted and the rest ignored.
I cannot use a LEFT/SUBSTRING function as I am trying to fetch data from a linked server.

Thanks.
Thom A
Thom A
SSC Guru
SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)SSC Guru (82K reputation)

Group: General Forum Members
Points: 82225 Visits: 21073
funooni - Sunday, January 21, 2018 6:23 AM
Thanks Thom,

But this would just disappear the warning. Let me make it more clear about what i am looking for



create table TEST_INSERT
(id int , name varchar (3))

GO


INSERT INTO TEST_INSERT
SELECT 1, 'FOUR'

In case the warnings are on i get the error, otherwise it says completed successfully and in both cases nothing gets inserted.
What I am looking for is to have the first 3 characters inserted and the rest ignored.
I cannot use a LEFT/SUBSTRING function as I am trying to fetch data from a linked server.

Thanks.


Have you tried what I provided? And what does using a linked server have to do with not being able to use LEFT?



Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does :-P

Please always remember to encapsulate your code in IFCode Markup. For example [code=sql] [/code].
Click here to read Jeffs Guide on how to post SQL questions, and get swift and helpful answers from the community
Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)

Group: General Forum Members
Points: 214384 Visits: 24732
[quote]
funooni - Sunday, January 21, 2018 6:23 AM
What I am looking for is to have the first 3 characters inserted and the rest ignored.

As far as I know, there is no setting which will allow this to happen. You need to fix the data prior to insert.



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
HappyGeek
HappyGeek
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14759 Visits: 7218
Thom A - Sunday, January 21, 2018 6:43 AM


Have you tried what I provided? And what does using a linked server have to do with not being able to use LEFT?

+1, can't see issue.


...
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search