How Character Strings Are Compared
When comparing two character strings, it is important to consider the range of ASCII characters permitted by a given data type. Including non‑printable characters in varchar or char comparison strings affects the outcome of the comparison.
Blanks are not ignored in comparisons by either text or varchar. However, the way blanks are handled by the two data types differs. In comparing strings of unequal length, varchar effectively adds blanks at the end of the shorter string to bring it to the same length as the longer string. The text data type does not add blanks; it considers a shorter string as less than a longer string if all characters up to the length of the shorter string are equal.
As an example of the way this affects comparisons, consider the following two strings:
abcd\001
abcd
Assume that \001 represents one ASCII character, Control‑A. If these are compared as text, the first string is greater than the second. However, if they are compared as varchar, the first string is less than the second, because the blanks added by varchar to the shorter string have a higher ASCII value than 001.
The following table summarizes the various character data types, where n represents the lesser of the maximum configured row size and 32,000: