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: 

Choosing the highest alpha numeric value in itab and display in variable

former_member267445
Participant
0 Kudos
115

Hello Gurus,

We are having one custom table and am picking the data from that table into itab, itab contains the values like below

field1                    field2          field3                    field4     field5     field6

A071458              Retail         0005002694         1200     Y010     1200000000

CMR1202576      CMR          0005002694          1100     Y001     1100000000

CMR1706299      CMR          0005002694          1100     Y001      1100000000

CMR3001127      CMR          0005002694          1100     Y001      1100000000

CMR3001991      CMR          0005002694          1100     Y001      1100000000

L018879              Retail         0005002694          1100     Y003      1100000000

from the above internal table i have to fetch the field1 starting with CMR30 and i have to pass the highest value from them into one variable. Means

field1

CMR3001127

CMR3001991  from these i have to find highest value and pass it to another variable. As i am fresher am not able to find the solution.

Please light me on this.

Thanks and Regards,

Muralikrishna Peravali

1 ACCEPTED SOLUTION

Former Member
0 Kudos
78

You can sort the internal table in descending order and read first record that matches pattern.

Something like this:

SORT itab BY field1 DESCENDING.

LOOP AT itab INTO workarea.

  IF workarea-field1+0(5) EQ 'CMR30'.

    "the value you want.

    EXIT.

  ENDIF.

ENDLOOP.

7 REPLIES 7

Former Member
0 Kudos
79

You can sort the internal table in descending order and read first record that matches pattern.

Something like this:

SORT itab BY field1 DESCENDING.

LOOP AT itab INTO workarea.

  IF workarea-field1+0(5) EQ 'CMR30'.

    "the value you want.

    EXIT.

  ENDIF.

ENDLOOP.

0 Kudos
78

Hi Manish Kumar,

Thankyou for the response. Actually in that table we have Cusotmer number. So based on Customer number will your code work?

Please advise me.

Thanks and Regards,

Muralikrishna Peravali

0 Kudos
78

It should work.

You can try it out and post the result and code if it doesn't work.

Former Member
0 Kudos
78

Hi MuraliKrishna,

You can also try using the below logic.

  TYPES : BEGIN OF typ_test,
          char(10),
  END OF typ_test.

DATA : wa TYPE typ_test,
            itab TYPE TABLE OF typ_test,
            high(10).

wa-char = 'CMR3001127'.
APPEND wa TO itab.
wa-char = 'CMR3001991'.
APPEND wa TO itab.
wa-char = 'CMS3001992'.
APPEND wa TO itab.
wa-char = 'CMR3001992'.
APPEND wa TO itab.

   LOOP AT itab INTO wa WHERE char+0(5) = 'CMR30'.
       IF wa-char+5(5) > high+5(5).
         high = wa-char.
       ENDIF.
    ENDLOOP.

   WRITE : high.

***************************

output : CMR3001992

Regards,

Archana

0 Kudos
78

That would be really handy when internal table has millions of records.

former_member209120
Active Contributor
0 Kudos
78

Hi Muralikrishna,

It is working fine.... follow this way....

TYPES : BEGIN OF ty_itab,
        a(20) TYPE c,
        b(20) TYPE c,
        END OF ty_itab.


DATA : it_itab TYPE TABLE OF ty_itab,
        wa_itab TYPE ty_itab.

wa_itab-a = 'CMR1202576'. wa_itab-b = 'CMR'. APPEND wa_itab TO it_itab. CLEAR wa_itab.
wa_itab-a = 'CMR3001127'. wa_itab-b = 'CMR'. APPEND wa_itab TO it_itab. CLEAR wa_itab.
wa_itab-a = 'CMR3001991'. wa_itab-b = 'CMR'. APPEND wa_itab TO it_itab. CLEAR wa_itab.
wa_itab-a = 'CMR1706299'. wa_itab-b = 'CMR'. APPEND wa_itab TO it_itab. CLEAR wa_itab.

LOOP AT it_itab INTO wa_itab.
   IF wa_itab-a+0(5) NE 'CMR30'.
   delete it_itab .
   ENDIF.
ENDLOOP.


SORT it_itab BY a DESCENDING.
clear wa_itab.

read table it_itab into wa_itab INDEX 1.

WRITE : / wa_itab-a.


former_member198834
Participant
0 Kudos
78

hi

1. sort itab by descending

2. read table i.e first in itab is a biggest value

Suresh