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 Type of Elementary Data Objects
rel_exp - Comparison Type of Character-Like Data Objects
The following tables show the comparison types for comparisons between character-like data types and other data types. If the type of an operand is not the same as the comparison type, it is converted to this type. The comparison rules for the comparison types determine how the comparison is performed. If no comparison type is specified for a combination, then no comparison is possible.
Other versions: 7.31 | 7.40 | 7.54
Comparisons with Numeric Data Types
| - | string, c, n |
|---|---|
| decfloat16, decfloat34 | decfloat34 |
| f | f |
| p | p |
| int8 | int8 |
| i, s, b | i |
Value Ranges and Length Adjustments
- When the types
stringandcare compared with packed numbers of the typep, the comparison typephas 31 places together with the number of decimal places of the operand of typep. This can raise exceptions if overflows occur.
- When the type
nis compared with packed numbers of the typep, the numeric text can contain up to 31 digits, excluding leading zeros and regardless of how many decimal places are in the operand with typep.
- When the types
string,c, andnare compared with integers of the typesint8,i,s, andb, the number value in the character-like operand does not need to fit the value range of the comparison typei. If the number value is not in the value range, the comparison produces the correct result and no exception is raised.
Example
Compare a numerical text with an integer. If numtext contains an invalid value, this results in a non-handleable exception with the runtime error CONVT_NO_NUMBER.
DATA numtext TYPE n LENGTH 3.
cl_demo_input=>add_field( CHANGING field = numtext ).
DATA num TYPE int1.
cl_demo_input=>request( CHANGING field = num ).
cl_demo_output=>display(
COND #( WHEN numtext < num THEN |{ numtext } < { num }|
WHEN numtext > num THEN |{ numtext } > { num }|
ELSE |{ numtext } = { num }| ) ).
Comparisons with Character-Like Data Types
| - | string | c | n |
|---|---|---|---|
| string | string |
string |
p |
| c | string |
c |
p |
| n | p |
p |
n |
Length Adjustments
- Operands with a different length of data type
stringnever match. If the contents of the operands match for the length of the shorter operand, the shorter operand is smaller than the longer one. Otherwise the surplus places in the longer field are truncated on the right, and then the content is compared.
- In comparisons between two operands of data type
cwith the same length, the entire length is compared, which means the trailing blanks are respected. In comparisons between two operands of data typecwith different lengths, the shorter field is converted to the longer field, with blanks used as padding on the right.
- In comparisons between two operands of data type
n, the shorter field is converted to the longer field, with the character "0" used as padding on the left.
Note
In comparisons between text fields of the type c and text strings of the
type string, any trailing blanks are ignored by conversions from c
to string. This can have unexpected results. The conversion operator
CONV can be used here to force other comparison types (see the example after the link).
Example
In the following comparison, the comparison type is c and the comparison
is made in the current code page, in accordance with the binary representation. In most code pages,
"a" is greater than "Z". See also the example for CONVERT TEXT.
IF 'a' > 'Z'.
cl_demo_output=>display_text( `'a' > 'Z'` ).
ELSE.
cl_demo_output=>display_text( `'a' < 'Z'` ).
ENDIF.
Example
The following comparison is false, which is probably unexpected. The value returned by
boolc has the type string and includes a blank,
whereas the constant abap_false has the type c.
In the comparison, the value of abap_false is converted to an empty string, since the blank it contains is ignored.
IF boolc( 1 = 2 ) = abap_false.
cl_demo_output=>display_text( 'yes' ).
ELSE.
cl_demo_output=>display_text( 'no' ).
ENDIF.
The following comparison, however, is true, since the return value of
xsdbool has the same ABAP type as the constant abap_false.
IF xsdbool( 1 = 2 ) = abap_false.
cl_demo_output=>display_text( 'yes' ).
ELSE.
cl_demo_output=>display_text( 'no' ).
ENDIF.
Executable Example
Comparing Text Strings of Different Length
Comparisons with Byte-Like Data Types
| - | string | c | n |
|---|---|---|---|
| xstring | string |
string |
p |
| x | string |
c |
p |
Length Adjustments
- When data type
cis compared withxor xstring, the shorter field is adjusted to the length of the longer field after conversions fromxtocorxstringtostring. Blanks are used as filler on the right.
- Lengths are not adjusted for comparisons between the data type
stringandxorxstring.
Example
The logical expression CONV xstring( 255 ) = 'FF' is true.
ASSERT CONV xstring( 255 ) = 'FF'.
Comparisons with Date Types/Time Types/Time Stamp Types
| - | string | c | n |
|---|---|---|---|
| d, t | string |
c |
n |
| utclong | utclong |
utclong |
- |
Length Adjustments
- For comparisons between data types
c,n, orstringon the one hand anddon the other, the longer field is truncated on the right to the length of the shorter field, as long as only blanks are cut off.
- For comparisons between data types
cornon the one hand andton the other, the longer field is truncated on the right to the length of the shorter field, as long as only blanks are cut off.
- Lengths are not adjusted for comparisons between the data type
stringand the data typet.
Example
The example shows the length adjustment in a comparison of a time field with a character string. Both comparisons are true.
IF CONV t( '120000' ) = '12'.
cl_demo_output=>write( |CONV t( '120000' ) = '12'| ).
ENDIF.
IF CONV t( '123000' ) <> '12'.
cl_demo_output=>write( |CONV t( '123000' ) <> '12'| ).
ENDIF.
cl_demo_output=>display( ).
Example
The following comparison is always true. The initial text string literal is converted to the initial value of a time stamp field and this is less than any real time stamp.
ASSERT utclong_current( ) > ``.