3. Understanding SQL Data Types : SQL Data Types : Character Data Types : Varchar Data Type
 
Share this page                  
Varchar Data Type
Varchar strings are variable-length strings, stored as a 2-byte (smallint) length specifier followed by data. In uncompressed tables, varchar columns occupy their declared length. For example, if ABC is entered into a varchar(5) column, the stored result is:
'03ABCxx'
where:
03 is a 2-byte length specifier.
ABC is three bytes of data.
xx represents two bytes containing unknown (and irrelevant) data.
If the column is nullable, varchar columns require an additional byte of storage.
In compressed tables, varchar columns are stripped of trailing data. For example, if “ABC” is entered into a varchar(5) column in a compressed table, the stored result is:
'03ABC'
The varchar data type can contain any character, including non-printing characters and the ASCII null character ('\0').
Except when comparing with C data, blanks are significant in the varchar data type. For example, the following two varchar strings are not considered equal:
'the store is closed'
and
'thestoreisclosed'
If the strings being compared are unequal in length, the shorter string is padded with trailing blanks until it equals the length of the longer string.
For example, consider the following two strings:
'abcd\001'
where:
'\001' represents one ASCII character (ControlA)
and
'abcd'
If they are compared as varchar data types, then
'abcd' > 'abcd\001'
because the blank character added to 'abcd' to make the strings the same length has a higher value than ControlA ('\040' is greater than '\001').