Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How rigid is the max size of a VARCHAR in SQL Server?

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?

like image 903
Robert Morgan Avatar asked Dec 30 '25 19:12

Robert Morgan


1 Answers

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
------------------------------------------------
like image 187
Raj More Avatar answered Jan 02 '26 01:01

Raj More



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!