Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Silly question: Compare String with Integer

oliver_am
Active Participant
0 Kudos
1,465

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 !

1 ACCEPTED SOLUTION

DominikTylczyn
Active Contributor
1,087

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

8 REPLIES 8

AndyBoolean
Participant
0 Kudos
1,087

Hi oliver.am,

What is the value in the variable "string2" at runtime?

oliver_am
Active Participant
0 Kudos
1,087

This values

DominikTylczyn
Active Contributor
1,088

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

1,087

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

jrgkraus
Active Contributor
1,087

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 ).

(...)

touzik_itc
Active Participant
0 Kudos
1,087

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." ...

0 Kudos
1,087

You can find the conversion rule in the Abap online documentation, here: numeric to character type fields: Character-Like Target Fields

jack_graus2
Active Contributor
0 Kudos
1,087

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.