2023 Jan 11 12:09 PM
Hello, Ok, I know this is a really newbie question but I cannot find a solution...
Why this code is returning false?
REPORT ztest.
DATA:
string1 TYPE string,
string2 TYPE string,
integer TYPE int4 VALUE '15'.
string1 = '15'.
string2 = integer.
IF string1 = string2. "integer
WRITE: 'True'.
ELSE.
WRITE: 'False'.
ENDIF.
I need to compare a value in a string with the same value in an integer...
SAP_ABAP 701 cannot use any new features of abap syntaxis... (conv,etc...)
Thanks !
2023 Jan 11 12:38 PM
Hello oliver.am
Check the report under debugger and examine the values of STRING1 and STRING2 variables. It's evident why the comparison yields FALSE:
STRING2 is 3 chars long and differs from STRING1 by the trailing SPACE.
Why don't you compare integers instead of strings, e.g.:
DATA:
string TYPE string,
integer1 TYPE int4 VALUE '15',
integer2 type int4.
string = '15'.
CATCH SYSTEM-EXCEPTIONS CONVT_NO_NUMBER = 1.
integer2 = string.
ENDCATCH.
IF integer1 = integer2 and sy-subrc <> 1. "integer
WRITE: 'True'.
ELSE.
WRITE: 'False'.
ENDIF.
Best regards
Dominik Tylczynski
2023 Jan 11 12:34 PM
Hi oliver.am,
What is the value in the variable "string2" at runtime?
2023 Jan 11 12:38 PM
2023 Jan 11 12:38 PM
Hello oliver.am
Check the report under debugger and examine the values of STRING1 and STRING2 variables. It's evident why the comparison yields FALSE:
STRING2 is 3 chars long and differs from STRING1 by the trailing SPACE.
Why don't you compare integers instead of strings, e.g.:
DATA:
string TYPE string,
integer1 TYPE int4 VALUE '15',
integer2 type int4.
string = '15'.
CATCH SYSTEM-EXCEPTIONS CONVT_NO_NUMBER = 1.
integer2 = string.
ENDCATCH.
IF integer1 = integer2 and sy-subrc <> 1. "integer
WRITE: 'True'.
ELSE.
WRITE: 'False'.
ENDIF.
Best regards
Dominik Tylczynski
2023 Jan 11 2:34 PM
Thanks for answer. I didnt see any trailing space in the debugger... but it seems that there are one space ...
Why is added this space ??
with condense no-gaps is working fine now.
I cannot use your solution because in my real program the string2 not always contain a number... I can use CS '01234... by the moment i can continue with this, thaks
2023 Jan 11 1:20 PM
Strings containing numbers may be formatted different. ABAP puts the sign of an integer as additional character at the end of the text when converting integers to text. A string will keep this space character so you are comparing '15' and '15 ' which is not the same.
I would always use a number conversion when treating strings as integers such as
try.
if conv i( string1 ) = conv i( string2 ).
(...)
2023 Jan 11 1:27 PM
Two variables can be compared directly. This code returns 'True':
IF string1 = integer.
WRITE: 'True'.
ELSE.
WRITE: 'False'.
ENDIF.
If you need to compare two strings, you could add CONDENSE to trim a trailing space.
CONDENSE string2.
IF string1 = string2. "integer
WRITE: 'True'.
ELSE.
WRITE: 'False'.
ENDIF.
An assignment integer values to stings always uses the commercial notation. That is why an to assign and compare is not the same as to compare with an implicit conversion:
... "The value of the integer is formatted in commercial notation and passed, without gaps and without decimal separators, to the target field. The character "-" is set in the last place for a negative value and a blank is set for a positive value. The resulting length of the target field is determined by the number of digits plus the place for the sign." ...2023 Jan 11 4:54 PM
You can find the conversion rule in the Abap online documentation, here: numeric to character type fields: Character-Like Target Fields
2023 Feb 07 4:58 AM
Next to converting into integer you might convert into correct string:
string1 = '15'.
string2 = condense( string1 ).
IF string1 = string2. "integer
WRITE: 'True'.
ELSE.
WRITE: 'False'.
ENDIF.