views:

830

answers:

3

In sql server does it make a difference if I define a varchar column to be of length 32 or 128?

+2  A: 

There should be no noticeable difference as the backend will only store the amount of data you insert into that column. It's not padded out to the full size of the field like it is with a char column.

Edit: For more info, this link should be useful.

Jarod Elliott
+2  A: 

It should not. It just defines the maximum length it can accommodate, the actual used length depends on the data inserted.

Nrj
+5  A: 

A varchar is a variable character field. This means it can hold text data to a certain length. A varchar(32) can only hold 32 characters, whereas a varchar(128) can hold 128 characters. If I tried to input "12345" into a varchar(3) field; this is the data that will be stored:

"123"

The "45" will be "truncated" (lost).

They are very useful in instances where you know that a certain field will only be (or only should be) a certain length at maximum. For example: a zip code or state abbreviation. In fact, they are generally used for almost all types of text data (such as names/addresses/excreta) - but in these instances you must be careful that the number you supply is a sane maximum for the type of data that will fill that column.

However, you must also be careful when using them to only allow the user to input the maximum amount of characters that the field will support. Otherwise it may lend to confusion when it truncates the user's input.

Here is the official documentation of this datatype:

http://msdn.microsoft.com/en-us/library/aa258242(SQL.80).aspx

nlaq