Tech moves fast! Stay ahead of the curve with Techopedia!
Join nearly 200,000 subscribers who receive actionable tech insights from Techopedia.
A variable character field (varchar) is a data type which can contain any type of data: numeric, characters, spaces or punctuation. Depending on the database, the data type is capable of storing values up to its maximum size. Variable character fields are mostly used to act as temporary variables and for string operations. They bring in much-needed versatility for data field types.
A variable character field can be defined either in the programming language or on the database level. A variable character field always has a declared maximum length and is usually initialized with the current length equal to zero. The current length of the variable character field can be anything from zero to the maximum declared field length. The method of declaring a variable character field differs according to the programming language used.
One of the biggest advantages of variable character fields is the avoidance of padding. In the case of a character field, the field occupies the exact number of characters regardless of the actual size of the string, and the difference between the fixed length and field length is padded with spaces. Unlike a character field, a variable character field only utilizes the space required for the size of the string, so only the minimum storage space is required. It helps in avoiding any wastage, and this feature is helpful in searching and sorting of values. In some databases and programming languages, any extra space found is automatically removed before storing to the database.
Based on the database or the programming language, there could be limitations on a variable character field, such as it cannot be used with select statement or cannot be used as a candidate or primary key.
Most relational database management systems support the variable character field.