Size of varchar columns

前端 未结 3 837
执笔经年
执笔经年 2021-01-17 15:32

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

相关标签:
3条回答
  • 2021-01-17 15:50

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

    0 讨论(0)
  • 2021-01-17 15:59

    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.

    0 讨论(0)
  • 2021-01-17 16:01

    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.

    0 讨论(0)
提交回复
热议问题