Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Program Flow Logic →  Expressions and Functions for Conditions →  log_exp - Logical Expressions →  rel_exp - Comparison Expressions →  rel_exp - Comparison Rules →  rel_exp - Comparing Elementary Data Types →  rel_exp - Comparison Type of Elementary Data Objects 

rel_exp - Comparison Type of Character-Like Data Objects

The following tables show the comparison types for comparisons between character-like data types and other data types. If the type of an operand is not the same as the comparison type, then it is converted to this type. The comparison rules for the comparison types determine how the comparison is performed. If no comparison type is specified for a combination, then no comparison is possible.

Other versions: 7.31 | 7.40 | 7.54

Comparisons with Numeric Data Types

- string, c, n d,t
decfloat34 decfloat34 decfloat34
decfloat16 decfloat34 decfloat34
f f f
p p p
i i i
s i i
b i i

Value Ranges and Length Adjustments

  • When the types string and c are compared with packed numbers of the type p, the comparison type p has 31 decimal places together with the decimal portion of the operand of type p. This can raise exceptions if overflows occur.

  • When the type n is compared with packed numbers of the type p, the numeric text can contain up to 31 places, excluding leading zeroes and regardless of how big the decimal portion of the type p operand is.

  • When the types string, c, and n are compared with whole numbers of the types i, s, and b, the number value in the character-like operand does not need to fit the value range of the comparison type i. If the number value is not in the value range, the comparison produces the correct result and no exception is raised.

Comparisons with Character-Like Data Types

- string c n d t
string string string p string string
c string c p c c
n p p n n n
d string c n d -
t string c n - t

Length Adjustments

  • Operands with a different length of data type string never match. If the contents of the operands match across the length of the shorter operand, the shorter operand is smaller than the longer one. Otherwise the surplus places in the longer field are truncated on the right, and then the content is compared.

  • For comparisons between two operands of data type c, the shorter field is converted to the longer field, with blanks used as padding on the right.

  • For comparisons between two operands of data type n, the shorter field is converted to the longer field, with the character "0" used as padding on the left.

  • For comparisons between data types c, n, or string on the one hand and d on the other, the longer field is truncated on the right to the length of the shorter field, as long as only blanks are truncated.

  • For comparisons between data types c or n on the one hand and t on the other, the longer field is truncated on the right to the length of the shorter field, as long as only blanks are truncated.

  • Lengths are not adjusted for comparisons between the data type string and the data type t.

Note

For comparisons between text fields of the type c and text strings of the type string, note that trailing blanks are ignored by conversions from c to string. This can have unexpected results. The conversion operator CONV can be used here to force other comparison types (see the example after the link).


Example

In the following comparison, the comparison type is c and the comparison takes places in the current code page, in accordance with the binary representation. In most code pages, "a" is greater than "Z". See also the example for CONVERT TEXT.

IF 'a' > 'Z'. 
  cl_demo_output=>display_text( `'a' > 'Z'` ). 
ELSE. 
  cl_demo_output=>display_text( `'a' < 'Z'` ). 
ENDIF.

Example

The following comparison is false, which is probably unexpected. The value returned by boolc has the type string and includes a blank, whereas the constant abap_false has the type c. For the comparison, the value of abap_false is converted to an empty string, since the blank it contains is ignored.

IF boolc( 1 = 2 ) = abap_false. 
  cl_demo_output=>display_text( 'yes' ). 
ELSE. 
  cl_demo_output=>display_text( 'no' ). 
ENDIF.

The following comparison, however, is true, since the return value of xsdbool has the same ABAP type as the constant abap_false.

IF xsdbool( 1 = 2 ) = abap_false. 
  cl_demo_output=>display_text( 'yes' ). 
ELSE. 
  cl_demo_output=>display_text( 'no' ). 
ENDIF.

Comparisons with Byte-Like Data Types

- string c n d,t
xstring string string p i
x string c p i

Length Adjustments

  • When data type c is compared with x or xstring, the shorter field is adjusted to the length of the longer field after conversions from x to c or xstring to string. Blanks are used as filler on the right.

  • Lengths are not adjusted for comparisons between the data type string and x or xstring.