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: 

IF statement where String "starts with"

simantini_sh
Explorer

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.

1 ACCEPTED SOLUTION

LaurensDeprost
Contributor

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

LaurensDeprost
Contributor

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

could be much beautiful with a little COND

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

You don't see ? the beautiful of the simplification 🙂

matt
Active Contributor

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.

Nikhil_Rao_ABAP
Participant
0 Kudos

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

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.

0 Kudos

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!!

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.

ThorstenHoefer
Active Contributor

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.

matt
Active Contributor

Next time, answer your own question by putting the cursor on EQ and pressing F1 to call up the ABAP language help.

Sandra_Rossi
Active Contributor

or even search the Web, this question has been asked and answered already:

IF statement where String "starts with" site:sap.com

matt
Active Contributor
  • Post question, wait an hour for an answer...
  • Spend five minutes reading the documentation.

I wonder which is more efficient?