‎2010 Nov 05 10:05 AM
Hi,
I need to compare two character variables of type CHAR20 in "string mode" in if condition:
example:
v1 has value P123
v2 has value P00001.
if v1 > v2.
do something.
endif.
In this case if condition should be false. But its true.. How to solve?
Thank you for answers.
‎2010 Nov 05 10:11 AM
Why should this be false? Do you want to compare the string length ?
If yes, this is very basic, please search for available information.
If not, what do you want, please explain the underlying logic.
Thomas
‎2010 Nov 05 10:10 AM
I'm not sure what you mean with 'string mode'.
But.. looking at your example: no way is that ever going to be false.
You're comparing character values and not numerical values.
Edited by: Maen Anachronos on Nov 5, 2010 11:11 AM
‎2010 Nov 05 10:11 AM
Why should this be false? Do you want to compare the string length ?
If yes, this is very basic, please search for available information.
If not, what do you want, please explain the underlying logic.
Thomas
‎2010 Nov 05 10:50 AM
Hi,
string comparison means I think:
1. If both string have same lenght compare content
2. if not, longer string is "bigger"
so:
P123 < P0001 - because of lenght
P0002 > P0001 - because of content
A0001 < P0002 - because of content
Thanks for reply
‎2010 Nov 05 11:41 AM
Length comparion: you can solve that pretty easy yourself (hint: STRLEN).
As for the content comparison: since they are only characters and not numbers my first intention would be to put the two values into an internal table and then sort the internal table ascending (or descending if you choose).
After the sort the first entry in your internal table will hold the 'lowest' value and the second enrty will hold the 'highest'.
‎2010 Nov 05 11:53 AM
Hi Maen,
thanks for reply, but I hope there must be a better solution in ABAP...
‎2010 Nov 05 12:49 PM
There is. Convert the values to XSTRING and then your IF comparison will work.
data: gv1 type xstring,
gv2 type xstring,
gv3(10) type c,
gv4(10) type c.
gv3 = 'P0002'.
gv4 = 'P0001'.
CONVERT TEXT gv3 INTO SORTABLE CODE gv1.
CONVERT TEXT gv4 INTO SORTABLE CODE gv2.
if gv1 > gv2.
write: /'GV1 > GV2'.
elseif gv1 < gv2.
write: /'GV1 < GV2'.
else.
write: /'GV1 = GV2'.
endif.Edit: mind you, this only works if the 2 values have the same length!
Edited by: Maen Anachronos on Nov 5, 2010 2:18 PM
‎2010 Nov 05 12:52 PM
Hi,
> 1. If both string have same lenght compare content
Just use the comparison operators < (lower than) and > (greater than)
..
> 2. if not, longer string is "bigger"
Length comparion: you can solve that pretty easy yourself (hint: STRLEN).
http://help.sap.com/abapdocu_70/en/ABENLOGEXP_ANY_OPERAND.htm
I hope this helps,
Paulo.
‎2010 Nov 05 10:49 AM
Hi
If you want to compare the contents of the two chararcter variables i would suggest moving them to numberic fields and doing a comparison there where relational operators can work with ease. However if the contents of the character fields contains non-numeric data as well and you know the space occupied by this then you can pass only the numeric data for comparison.
data: v1 type char20 value 'P123',
v2 type char20 value 'P000001',
val1 type p decimals 2,
val2 type p decimals 2.
move: v1+1(19) to val1,
v2+1(19) to val2.
if val1 > val2.
*go to the moon
endif.
Please note you have to have a numeric field which can cater for 19 characters to avoid an over flow
regards
Isaac Prince
Edited by: prince isaac on Nov 5, 2010 12:49 PM
‎2010 Nov 05 3:08 PM
Hi,
it compares string by position from left:
P123
P00001
1/First postion V1 = P
V2 = P
it is same
2/Second position: V1 = 1
V2 = 0
V1 > V2 is true for this position
If you would like to compare numbers 123 and 1.
Get rid of characters from you string 'P'
create two integer variables
I1 = v1.
I2 = v2
I1 = 123
I2 = 1
And now when you compare I1> I2 condition will be false for your example.
Bye Jan
‎2010 Nov 05 3:12 PM
Why don't you give my example a try? You'll see how surprisingly easy it is without all the offsets and other stuff (what if the first char is not a P? what if the string holds P101P101? What if....
I myself was very surprised to find this functionality in ABAP.