Logical Expressions - Relational Operators For All Data Types

In some cases, the syntax rules that apply to Unicode programs are different than those for non-Unicode programs. See Comparisons in Unicode Programs.

In the expression "f1 op f2", op can be any of the following operators, regardless of the data type involved:

=, EQ: Equal to
  <>, NE: Not equal to
  >, GT: Greater than
  <, LT: Less than
  >=, GE: Greater than or equal to
  <=, LE: Less than or equal to

The syntax check performed in an ABAP Objects context is stricter than in other ABAP areas.

See Incorrect logical operators.

If f1 and f2 have the same type and length, the comparison is performed as follows. Number fields, i.e. fields of type P, I or F, are compared according to their number value.
With all other field types, the comparison is executed from left to right, and the first different character on the left decides which operand is greater.
For the individual types, this process works in the following manner. Text fields (type C and STRING are compared lexicographically with reference to the underlying character code.
With date fields (type D), the more recent date is "greater" than the older date. With time fields, (type T), the later time is "greater" than the earlier time.
If you interpret type N fields as positive integers, the greater number is also "greater" than the other number.
Hex fields (type X and XSTRING) are compared byte by byte, and the result of the comparison depends on the value of the bytes as dual numbers.
The following rules apply to reference variables:
Data object reference variables are identical if they point to the same object. 'Same object' here means that the address in memory is the same and the data type is compatible. Data object references are not identical if they do not point to the same object. This means that two data object references are not identical if they point to the same address in memory but the referenced types are incompatible. For example, two reference variables, where one points to a structure and the other one to the first component of the structure, are not identical because the data type of the data objects is not compatible. The Less Than/Greater Than comparison for data object references is defined internally and always provides the same results in the same context. It can be used for sorted arrays of references, for example in internal tables to retrieve specific references.
Object references are identical if they point to the same object because the data type of objects in ABAP objects is always the same. For the Less Than/Greater Than comparison of object references, the same applies as for data object references.

If the relational operands f1 und f2 have different lengths but the same type, P fields are compared according to their number value. C strings are compared lexicographically; for C fields, however, the shorter field is implicitly padded to the right with blanks and then compared. X strings are compared byte by byte, the shorter string - if it is a prefix of the longer one - being the smaller one. For X fields the shorter field is implicitly padded to the right with hexadecimal zeros until both fields have the same length. Type N fields, however, are padded with '0' from the left.
If the comparison operands f1 and f2 are of different types, you can still perform a meaningful comparison by observing the following rules:

  1. If f1 or f2 is a field of type F, the other is first internally converted according to the rules for MOVE before the comparison is performed.


  2. If 1. does not apply and one of the fields for comparison is of type P, the other field is internally converted to type P (MOVE). If the program attribute "Fixed point arithmetic" is set, the system also takes into account the number of decimal places in the type P field. Type I fields are treated like type P fields (i.e. 0 decimal places).


  3. If neither 1. nor 2. apply and one of the fields for comparison is a date or time field, i.e. type D or T, the second field is internally converted to this type ( MOVE). The comparison of date fields with time fields is not supported and results in termination of the program at runtime.


  4. If one of the fields is of type N and the other of type X, C or XSTRING or STRING, respectively, both are internally converted to P (MOVE).


  5. Finally, if the comparison involves two fields of type C and X, the type X field is internally converted to C before the comparison (MOVE). If one operand has a variable-length type ( STRING or XSTRING), but the other one has not, the fixed-length operand is first converted to the corresponding variable-length type (MOVE).


If both of the relational operators are structures with different types that do not contain tables, they are regarded as fields with type C. These type C fields then form the basis for the comparison. If the structures have the same type, the system compares them component by component and according to the respective component types: In this case, the structures are identical if all of their components are identical. If they are not equal, the first component comparison that is unequal determines the result of the structure comparison (smaller or greater). Alignment gaps (if there are any) have no effect on the comparison.

Internal tables are compared sequentially line by line. Two internal tables are identical if they

If there are differences, the result of the table comparison is determined by

Nested internal tables are compared recursively. If t is an internal table with a header line, you refer to the table itself in comparisons with t[].

Note

Comparing floating point numbers:

Due to rounding errors when calculating and converting with floating point numbers (see ABAP Numeric Types), it rarely makes sense to test whether two floating point numbers are the same (=) zu testen (accuracy and rounding procedure are, in any case, hardware-dependent). Instead, you can check whether the relative difference is smaller than a predefined ceiling EPSILON. For example:

DATA: ACTUAL   TYPE F,
      TARGET   TYPE F,
      REL_ERR  TYPE F,
      EPSILON  TYPE F VALUE '0.00001'.

...
REL_ERR = ABS( ( ACTUAL - TARGET ) / TARGET ).
IF REL_ERR < EPSILON.
...


Additional help

Comparing Different DataTypes