Application Development and Automation 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: 
Read only

String pattern matching in where clause

matteo_montalto
Contributor
0 Likes
3,457

Hi all gurus,

my requirement: I'm working on an internal table and I have to loop on elements whose field MATNR meets a specific pattern.

MATNR is a string (CHAR40) and could contain both numeric digits and characters.

Supposing I get a NUMERIC field, say '1234', I have to loop on the itab just for the element which meets the following pattern:

X1234

where X could be a sequence of zeroes (even a null sequence). So, just to remain on the example,

MATNR = 1234

match the pattern, as well as MATNR = 0001234 and MATNR = 01234.

To provide another example, MATNR = 000A0001234 does NOT match the pattern, as well as MATNR = 1243 or MATNR = 000100001234.

I'm wondering if there's a way to render this pattern via operators in ABAP. As far as I've seen, the CP operator does not allow to render "an arbitrally long set of zeroes" expression.

Closest I can imagine is:

LOOP AT internal_table INTO workarea
           WHERE matnr CP pattern
             AND matnr NA SY-ABCDE.

where pattern = '*1234'. Such a clause is almost correct, as doesn't exclude cases like '00001000001234' as I can't find a way to tell the prefix should be a sequence of zeroes.

Any idea is welcome.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,385

Why dont you use MATNR as TYPE N (40) and have the same in the internal table?

So when you assign 1234 it will prefix 36 zeroes to the variable.

Thoughts?

6 REPLIES 6
Read only

Former Member
0 Likes
2,386

Why dont you use MATNR as TYPE N (40) and have the same in the internal table?

So when you assign 1234 it will prefix 36 zeroes to the variable.

Thoughts?

Read only

0 Likes
2,385

Hi Shambu,

unfortunately, I cannot change data declaration.

Actually, there's a workaround for the specific issue:

supposing: pattern = '*1234';

                 key = '1234'.

I could do as follows:

LOOP AT internal_table INTO workarea
           WHERE matnr CP pattern
             AND matnr NA SY-ABCDE.

    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
           EXPORTING
             input  = workarea-matnr
           IMPORTING
             output = tmp_prod_id.

    IF tmp_prod_id <> key.

       EXIT.

    ELSE.

      "code

....

Just for curiosity, I was looking if there's a way to render the condition on a specific WHERE statement clause.


Read only

0 Likes
2,385

I don't think there is one right at the WHERE-condition. In addition to your approach above, you could try finding a regular expression that serves your purpose and use it in a FIND statement inside the loop.

Thomas

Read only

Former Member
0 Likes
2,385

Hi,

Just try this simple program. I used conversion exit to remove leading zeros. Hope it helps.

DATA: itab TYPE TABLE OF makt,
       wa LIKE LINE OF itab.

DATA: g_matnr TYPE matnr VALUE '1234'.

wa-matnr = '000100001234'.
APPEND wa TO itab.

wa-matnr = '00A1234'.
APPEND wa TO itab.

wa-matnr = '0001234'.
APPEND wa TO itab.

wa-matnr = '01234'.
APPEND wa TO itab.

wa-matnr = '1243'.
APPEND wa TO itab.


CLEAR: wa.
LOOP AT itab INTO wa WHERE matnr NA sy-abcde.

   WRITE:/ wa-matnr.

   CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
     EXPORTING
       input  = wa-matnr
     IMPORTING
       output = wa-matnr.

   WRITE: wa-matnr.

   IF wa-matnr EQ g_matnr.
     WRITE: 'YES'.
   ELSE.
     WRITE: 'NO'.
   ENDIF.

ENDLOOP.

Sample Output:

Regards,
Jake

Read only

0 Likes
2,385

Jake, that's the workaround I used and described in my post above... Thanks anyway!

Kesavadas; unfortunately there's no fixed-number-of-digits for MATNR... I should then count the length of any key I have to process.

Anyway, thank you all, I desume there's no way to express such a pattern matching rule in the WHERE clause of the main loop. As far as my workaround works, I can close the discussion

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
2,385

if you have say lv_field1 = X1234.

Just consider the length as 4 removing the X. Now while looping just use the statement like

len1 = 4 "length of X1234

write lv_field into lv_field using edit mask '==ALPHA'.

len = strlen( lv_field ).

if len = len1.                  "if both lenght are same

then true

if lv_field1 = lv_field.       "if both values re same

then true.

else.

false.

else

false

endif.

Hope it helps.