2

i.e. if I create a VARCHAR(50) field, what happens if I try to assign it a value that's 100 characters long?

Will SQL Server let me do this? Is it somehow less efficient?

3 Answers 3

10

If you try forcing a large string into a smaller-size variable, the incoming string will be simply chopped off at the appropriate size.

declare @Variable varchar (50)
set @Variable = replace (SPACE (100), ' ' , '.')
print @Variable 

the output is 50 characters long

..................................................

If you try to force a large string into a small sized Field in a Table, an Error will be raised

declare @MyTable Table
(
    TestVariable varchar (50)
)
insert into @MyTable (TestVariable) select replace (SPACE (100), ' ' , '.')
select * from @MyTable 

the output is

Msg 8152, Level 16, State 14, Line 6
String or binary data would be truncated.
The statement has been terminated.
TestVariable
------------------------------------------------
0
3

It will throw an error saying that the value would be truncated. It won't write more than the 50 characters you've specified, otherwise you wouldn't be able to prevent the row size from growing out of control

1

Will SQL Server let me do this? Is it somehow less efficient?

It will emit an error, saying that the string or binary data would be truncated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.