Alphanumeric comparisons

An alphanumeric comparison is a comparison of the single-byte character values of two operands.

When one of the operands is neither class alphanumeric nor class alphabetic, that operand is processed as follows:

  • A display floating-point data item is treated as though it were a data item of category alphanumeric, rather than as a numeric value.
  • A zoned decimal integer operand is treated as though it were moved to a temporary elementary data item of category alphanumeric with a length the same as the total number of digits in the number, according to the rules of the MOVE statement.

    When the ZWB compiler option is in effect, the unsigned value of the integer operand is moved to the temporary data item. When the NOZWB compiler option is specified, the signed value is moved to the temporary data item. See ZWB in the Enterprise COBOL Programming Guide for more details about the ZWB (NOZWB) compiler option.

    Comparison then proceeds with the temporary data item of category alphanumeric.

Comparison of two alphanumeric operands

Alphanumeric comparisons are made with respect to the collating sequence of the character set in use as follows:

  • For the EBCDIC character set, the EBCDIC collating sequence is used.
  • For the ASCII character set, the ASCII collating sequence is used. (See EBCDIC and ASCII collating sequences.)
  • When the PROGRAM COLLATING SEQUENCE clause is specified in the object-computer paragraph, the collating sequence used is the one associated in the special-names paragraph with the specified alphabet-name.

The size of each operand is the total number of character positions in that operand; the size affects the result of the comparison. There are two cases to consider:

Operands of equal size
Characters in corresponding positions of the two operands are compared, beginning with the leftmost character and continuing through the rightmost character.

If all pairs of characters through the last pair evaluate as equal, the operands are equal.

If a pair of unequal characters is encountered, the characters are tested to determine their relative positions in the collating sequence. The operand that contains the character higher in the sequence is considered the greater operand.

Operands of unequal size
If the operands are of unequal size, the comparison is made as though the shorter operand were extended to the right with enough spaces to make the operands equal in size.

The higher collating value is determined using the hexadecimal value of characters.

Standard comparison

A standard comparison is any comparison that is not based on a locale. The standard comparison method depends on whether the operands to be compared are of equal length or unequal length.

If the operands are of unequal length, the comparison proceeds as though the shorter operand were padded on the right with appropriate space characters to make the operands of equal length. The comparison then proceeds according to the rules for the comparison of operands of equal length.

If the operands are of equal length, the comparison proceeds by comparing corresponding character positions in the two operands, starting from the leftmost position, until either unequal characters are encountered or the rightmost character position is reached, whichever comes first. The operands are determined to be equal if all corresponding characters are equal.

The first-encountered unequal character in the operands is compared to determine the relation of the operands. The operand that contains the character with the higher collating value is the greater operand.