Show TOC

Comparisons for Operands of Arbitrary Data TypesLocate this document in the navigation structure

For comparisons between operands of arbitrary data types, you have in ABAP the operators: =, <>, <, >, <=, >= or their equivalents EQ, NE, LT, GT, LE, GE at your disposal. We always recommend using either only the mathematical symbols or only the appropriate letter abbreviations. How the content comparison takes place depends on the data types of the operands.

Comparisons Between Different Data Types

For comparisons between operands of elementary data types, you must first decide whether the data types are compatible or not:

  • For compatible elementary data types, the content is compared without prior conversion.
  • For incompatible elementary data types, the content of the operands is first converted in accordance with certain rules and then compared.

The appropriate conversion rules and details on the rules concerning which operands of elementary data types in ABAP are to be compared can be found in the keyword documentation.

Tip

DATA: hex1(3) TYPE x VALUE '34B7A1',      hex2(1) TYPE x VALUE 'F0'.

IF hex2 > hex1.  ...ENDIF.

The logical expression in the IF statement is true, since the first byte of hex2 is greater than the first byte of hex1.

Tip

DATA: num(4)  TYPE n VALUE '1234',      text(5) TYPE c VALUE '567.8'.

IF num > text.  ...ENDIF.

The logical expression in the IF statement is true, since the value of num is greater than the value of text after the conversion to packed numbers. If num were of type C, the logical expression would be false, since there would be no conversion to a packed number.

Comparisons Between Reference Variables

When you compare compatible or convertible reference variables in ABAP Objects , it only makes sense to use the equal (EQ, =) and not-equal (NE, <>, ><) operators. The equality comparison is true if both reference variables contain references to the same object. It is false if they contain references to different objects. The converse is true if you are testing for inequality.

Tip

INTERFACE i1.  ...ENDINTERFACE.

CLASS c1 DEFINITION.  PUBLIC SECTION.  INTERFACES i1.  ...ENDCLASS.

DATA: r1 TYPE REF TO i1,      r2 TYPE REF TO c1.

CREATE OBJECT r2.

r1 = r2.

IF r1 = r2.  ...ENDIF.

The logical expression in the IF statement is true, since both references point to the same object.

Comparisons Between Structures

Compatiblestructures are broken down into their elementary components, which are then compared. Two structures are equal if all of their elementary components are equal. If the structures are unequal, the first pair of elements that are unequal determine which structure is larger.

Tip

DATA: BEGIN OF struct1,        col1 TYPE i VALUE 1,        col2 TYPE p DECIMALS 2 VALUE '56.78',      END OF struct1.

DATA: BEGIN OF struct2,        comp1 TYPE i VALUE 10,        comp2 TYPE p DECIMALS 2 VALUE '12.34',      END OF struct2.

IF struct1 < struct2.  ...ENDIF.

The logical expression in the IF statement is true, since the value of the first component of struct1 is less than the value of the second component.

When you compare incompatible structures, or compare structures with elementary fields, the structures involved are converted into type c fields before the conversion, and then treated like elementary fields.

Tip

DATA: BEGIN OF struct,        col1(5) TYPE c VALUE '12345',        col2(5) TYPE n VALUE '12345',      END OF struct.

DATA text(10) TYPE c VALUE '1234512345'.

IF struct = text.  ...ENDIF.

The logical expression in the IF statement is true, since structure struct has the same contents as the field text once it has been converted into a field of type c.

Comparisons Between Internal Tables

Like other data objects, you can use internal tables as operands in logical expressions.

If you are using internal tables with header lines, remember that the header line and the body of the table have the same name. If you want to address the body of the table in a comparison, you must place two brackets ([]) after the table name.