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 Rules for Comparison Types
When operands with elementary data types are compared, the comparison uses a comparison type defined by the operands in question, as for elementary data objects and calculation expressions. The comparison type can be one of the built-in ABAP types. When incompatible operands are compared, the operands that do not have the comparison type are converted to this type.
Other versions: 7.31 | 7.40 | 7.54
Numeric Comparison Type
If the comparison type is one of the numeric data types, the number values are compared.
Notes
- Platform-dependent rounding errors may occur with data type
f
, which means it often does not make sense to compare floating point numbers to see if they match.
Example
The comparison of a text string with an integer is performed with numeric comparison type i
.
If the text string cannot be converted to i
, a non-handleable exception would
be raised in the comparison. Convertibility is therefore checked first using the conversion operator CONV
.
DATA char TYPE string.
cl_demo_input=>add_field( CHANGING field = char ).
DATA num TYPE i.
cl_demo_input=>request( CHANGING field = num ).
TRY.
DATA(test) = CONV i( char ).
CATCH cx_sy_conversion_error.
cl_demo_output=>display( `Try again!` ).
RETURN.
ENDTRY.
cl_demo_output=>display(
COND #( WHEN char > num THEN `CHAR is greater than NUM`
WHEN char = num THEN `CHAR equals NUM`
ELSE `CHAR is lower than NUM` ) ).
Character-Like Comparison Type
If the comparison type is one of the character-like data types, the content is compared from left to right. Based on the internal binary representation in the code page used, the first differing character from the left determines which operand is greater.
Notes
- For operands of types
c
andstring
, the content is not compared on the basis of the locale of the current text environment. The statementCONVERT TEXT
can be used to specify the order with respect to the locale.
- If operands of type
n
contain a valid string of digits, the proportions of the numbers represented are determined correctly.
Example
The example shows whether the binary display of capitals is larger, the same or smaller than the binary display of small letters for individual characters. For the Unicode character display UCS-2 used by SAP, capitals are smaller than small letters. Numbers and special characters are not case-sensitive, and the result of the comparison for equality is true.
DATA char TYPE c LENGTH 1.
cl_demo_input=>request( CHANGING field = char ).
cl_demo_output=>display(
COND #( WHEN to_upper( char ) > to_lower( char )
THEN `Upper case is greater than lower case`
WHEN to_upper( char ) = to_lower( char )
THEN `Upper case is equal to lower case`
ELSE `Upper case is lower than lower case` ) ).
Byte-Like Comparison Type
If the comparison type is one of the byte-like data types, the content is compared from left to right. Based on the byte values, the first differing byte from the left determines which operand is greater.
Example
A byte-like comparison type can be achieved by comparing byte-like operands. Invalid values are compared into hexadecimal zeros here before being entered.
DATA hex1 TYPE x LENGTH 1.
cl_demo_input=>add_field( CHANGING field = hex1 ).
DATA hex2 TYPE x LENGTH 1.
cl_demo_input=>request( CHANGING field = hex2 ).
cl_demo_output=>display(
COND #( WHEN hex1 > hex2
THEN `HEX1 is greater than HEX2`
WHEN hex1 = hex2
THEN `HEX1 equals HEX2`
ELSE `HEX1 is lower than HEX2` ) ).
Date/Time Type as Comparison Type
If the comparison type is one of the date/time types, the content is compared from left to right. Based on the internal binary representation in the code page used, the first differing character from the left determines which operand is greater.
Note
For operands of types d
and t
containing a valid date or a valid time, the later date or time is always greater than the earlier one.
Example
Compares a date produced by adding a number value from the current date with the original date. The later date is always greater than the earlier one.
DATA(date) = sy-datlo.
cl_demo_input=>add_field( CHANGING field = date ).
DATA(days) = CONV int2( 0 ).
cl_demo_input=>request( CHANGING field = days ).
DATA(new_date) = CONV d( date + days ).
cl_demo_output=>display(
COND #( WHEN new_date > date
THEN `NEW_DATE is greater than DATE`
WHEN new_date = date
THEN `NEW_DATE equals DATE`
ELSE `NEW_DATE is lower than DATE` ) ).
Time Stamp Type as Comparison Type
If the comparison type is the time stamp type, the times are compared, with a later time always being the greater value and the initial value always being less than every real time stamp.