Muhammad Iqbal Muhammad Iqbal - 5 months ago 22
SQL Question

String or binary data would be truncated. The statement has been terminated. Simple Error

Create Table rs
(
Id int IDENTITY (1,1) Primary Key,
mId int Not NUll,
ad varchar Not NUll,
stvarchar Not NUll,
et varchar Not NUll,
nt varchar(max)
);


insert into rs ( nt, et, st, ad, mId)
values ('as','as','as','as',12)


I am getting this error

Msg 8152, Level 16, State 14, Line 2
String or binary data would be truncated.
The statement has been terminated.


It is a simple sql but finding difficulty in resolving it

Answer

From MSDN:

varchar [ ( n | max ) ]
Variable-length, non-Unicode string data. n defines the string length and can be a value from 1 through 8,000. max indicates that the maximum storage size is 2^31-1 bytes (2 GB).

Remarks When n is not specified in a data definition or variable declaration statement, the default length is 1. When n is not specified when using the CAST and CONVERT functions, the default length is 30.

specify Default length for Varchar .also look at this article..

   Create Table rs
   (
   Id int  IDENTITY (1,1) Primary Key,
   mId int Not NUll,
   ad varchar(5) Not NUll,
   st varchar(5) Not NUll,
   et varchar(5) Not NUll,
   nt varchar(max)
   );

   insert into rs ( nt, et, st, ad, mId) 
   values ('as','as','as','as',12