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:
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[].
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.
...
Comparing Different DataTypes