Size of varchar columns

Addi picture Addi · Oct 7, 2008 · Viewed 18.9k times · Source

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

Answer

user19302 picture user19302 · Oct 7, 2008

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/et cetera) - 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.