Entering content frameComparisons Between Different Data Types Locate the document in its SAP Library structure

Use the following logical operators to compare two data objects with different data types:

<operator>

Meaning

EQ

equal to

=

equal to

NE

not equal to

<>

not equal to

><

not equal to

LT

less than

<

less than

LE

less than or equal to

<=

less than or equal to

GT

greater than

>

greater than

GE

greater than or equal to

>=

greater than or equal to

Both operands must either be compatible or convertible.

Comparing Elementary Data Types

If a data object has one of the eight predefined ABAP types, it is compared as follows:

If the operands are compatible, there is no conversion. The procedure for the comparison is as follows: Numeric fields (types I, F, and P) and numeric strings (type N) are compared by their numeric values. For other data types (C, D, T, X), the comparison runs from left to right. The first character from the left that is different in each field determines which operand is greater. Text fields (type C) are compared based on the underlying character codes. In date field comparisons (type D), the more recent date is greater than the elder. In time field comparisons (type T), the later time is greater than the earlier. Hexadecimal fields (type X) are compared according to the values of their bytes.

When you compare incompatible operands with different lengths but the same data type, the comparison is as follows: Packed numbers (type P) are compared according to their numeric value without being converted. For the other types (C, N, X) with different lengths, the shorter operand is converted to the length of the longer before the comparison. It is then filled out as follows: Character strings (type C) are filled with spaces from the right, numeric strings (type N) are filled from the left with zeros, and hexadecimal fields are filled from the right with hexadecimal zero.

Example

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.

When you compare incompatible data objects whose data types are different, the system performs a type conversion before the comparison according to the following rules:

  1. If one of the operands is a floating point number (type F), the system also converts the other operands to type F.
  2. If one of the operands is a packed field (type P), the system also converts the other operands to type P.
  3. If one of the operands is a date field (type D) or a time field (type T), the system converts the other operands to type D or T respectively. Comparisons between date and time fields are not supported, and lead to a program termination.
  4. If one of the operands is a character field (type C) and the other operand is a hexadecimal field (type X), the system converts the operand of type X to type C.
  5. If one of the operands is a character field (type C) and the other a numeric field (type N), the system converts both operands to type P.

Example

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 had had type C, the logical expression would have been false, since there would have been no conversion to a packed number.

Comparing References

When you compare compatible or convertible reference variables in ABAP Objects, it only makes sense to use the equality (EQ or =) and inequality (NE, <>, or ><) 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.

Example

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.

Comparing Structures

Compatible structures 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.

Example

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 expression 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 are converted into type C fields before the conversion, and then treated like elementary fields.

Example

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 expression is true, since structure STRUCT has the same contents as the field TEXT once it has been converted into a field with type C.

Comparing Internal Tables

Refer to Comparing Internal Tables.

 

 

 

Leaving content frame