How do I disable errors on string truncation in SQL Server?

Ewan Makepeace picture Ewan Makepeace · Feb 9, 2009 · Viewed 23.5k times · Source

How can I tell SQL Server not to raise an error if I insert or update a string longer than the size of the field - I would like silent truncation in this instance.

Answer

SQLMenace picture SQLMenace · Feb 9, 2009

The thing you have to do is set ANSI WARNINGS to OFF You can do that by calling

set ANSI_WARNINGS  OFF

I have also written a practical example:

create table bla(id varchar(2))
go

insert bla values ('123') --fails


set ANSI_WARNINGS  OFF

insert bla values ('123') --succeeds

Do remember to turn the ANSI warnings back ON when you are done. You can do so by calling:

set ANSI_WARNINGS ON