Saturday, July 10, 2010

SQL SERVER – String or Binary Data would be Truncated

In SQL Server,While inserting the data in to a table using the insert statement, Occasionally we will see the following error message:

“String or Binary Data would be Truncated”

We will get this error, when the length of the value entered into a CHAR / VARCHAR / NCHAR / NVARCHAR column is longer than the maximum length of the column.

Let us see this through an example. First we will create  a table Product :

Create Table Product (ProdId INT,ProdName VARCHAR(16), ProdDesc VARCHAR(25))

Now inserting records,

Insert Into Product Values (1,'Adjustable Race' ,'Adjustable Race' )                                                                      Go                                                                         Insert Into Product Values (2,'Chain Ring Bolts' ,'Chain Ring Bolts' )                                                                      Go                                                                        Insert Into Product Values (3,'Chain Ring Nuts','Chain Ring Nuts')

On selecting the Records from the Table,

Select * From Product

image

Now we will insert one more record whose Product name length is longer than the maximum length of the column

Insert Into Product Values (4,'Headset Ball Bearings', 'Headset Ball Bearings')

whenever, we try to insert this record , it will throw an error specifying that “String or Binary Data would be Truncated” as the length of the Product name column is greater than the maximum length of the column.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.