07-09-2021 11:25 AM
In the below, code the codintion is, if POSID starts with 'G' the it will be No else Yes. I tried the code with "=" in place of EQ, but it's still not working. Please help
IF lw_prps-posid EQ 'G%'.
gw_list-w_chargeback = co_no. "NO
ELSE.
gw_list-w_chargeback = co_yes. "Yes
ENDIF.
07-09-2021 11:38 AM
Hi Simantini,
Try the 'CP' operator with the wildcard character ('*') instead of the 'EQ'/'=' operator:
IF lw_prps-posid CP 'G*'.
gw_list-w_chargeback = co_no. "NO
ELSE.
gw_list-w_chargeback = co_yes. "Yes
ENDIF.
Note that the '%' character can be used in OpenSQL statements together with the 'LIKE' operator to check if a field starts with a given letter
SELECT
"...
FROM prps
"INTO ...
"select records where POSID starts with 'G'
WHERE posid LIKE 'G%'.
You could also opt to use 'LIKE', '%' and the 'CASE' expression to directly determine the value for the 'chargeback' field in your selection.
SELECT pspnr, posid, post1,
"other WBS fields
CASE WHEN posid LIKE 'G%'
THEN @co_no
ELSE @co_yes
END AS w_chargeback
FROM prps
"WHERE ...
INTO TABLE @DATA(wbs_elements).
07-09-2021 11:38 AM
Hi Simantini,
Try the 'CP' operator with the wildcard character ('*') instead of the 'EQ'/'=' operator:
IF lw_prps-posid CP 'G*'.
gw_list-w_chargeback = co_no. "NO
ELSE.
gw_list-w_chargeback = co_yes. "Yes
ENDIF.
Note that the '%' character can be used in OpenSQL statements together with the 'LIKE' operator to check if a field starts with a given letter
SELECT
"...
FROM prps
"INTO ...
"select records where POSID starts with 'G'
WHERE posid LIKE 'G%'.
You could also opt to use 'LIKE', '%' and the 'CASE' expression to directly determine the value for the 'chargeback' field in your selection.
SELECT pspnr, posid, post1,
"other WBS fields
CASE WHEN posid LIKE 'G%'
THEN @co_no
ELSE @co_yes
END AS w_chargeback
FROM prps
"WHERE ...
INTO TABLE @DATA(wbs_elements).
07-09-2021 11:56 AM
07-09-2021 12:01 PM
frdric.girod what wouldn't be much more beautiful with a little COND 😉
gw_list-w_chargeback = COND #(
WHEN lw_prps-posid CP 'G*'
THEN co_no
ELSE co_yes ).
07-09-2021 12:14 PM
07-09-2021 5:34 PM
I think I'd go for
g_list_line-can_be_charged_back = xsdbool( lw_prps-posid CP 'G*' ).
Rename the field w_chargeback to can_be_charged_back, type abap_bool.
07-09-2021 11:52 AM
Sometimes I use ranges too to keep it simple( to avoid the confusion between * and %) -
DATA:
lw_prps TYPE prps,
lt_r_posid TYPE range of posid.
lt_r_posid[] = value #( ( sign = 'I' option = 'CP' low = 'G*' ) ).
IF lw_prps-posid in lt_r_posid[].
gw_list-w_chargeback = co_no. "NO
ELSE.
gw_list-w_chargeback = co_yes. "Yes
ENDIF.
Regards,
Nikhil
07-09-2021 1:10 PM
I'm so sorry, but you cannot keep things simple by adding more variables and lines of code.
If someone cannot understand
CP 'G*'
that someone doesn't need to keep things simple but a basic ABAP course.
07-20-2021 8:25 AM
Agreed Vincenc, thanks for that..... just meant to say, that I've always been more comfortable using ranges than the string comparison operators.... point taken, though!!
07-20-2021 10:29 AM
Just a quick point. With TYPE RANGE OF the [ ] is unnecessary.
DATA range_of_posid TYPE RANGE OF posid.
range_of_posid = value #( ( sign = 'I' option = 'CP' low = 'G*' ) ).
Also... using a range, CP is case insensitive. So CP 'G*' is to be preferred.
07-09-2021 1:02 PM
Hi,
you can check also the first character:
IF lw_prps-posid(1) = 'G'.
gw_list-w_chargeback = co_no. "NO
ELSE.
gw_list-w_chargeback = co_yes. "Yes
ENDIF.
07-09-2021 1:38 PM
Next time, answer your own question by putting the cursor on EQ and pressing F1 to call up the ABAP language help.
07-09-2021 1:56 PM
or even search the Web, this question has been asked and answered already:
IF statement where String "starts with" site:sap.com
07-09-2021 5:30 PM
I wonder which is more efficient?