Warm tip: This article is reproduced from stackoverflow.com, please click
abap range sap string

Split string mixed input into range of single values

发布于 2020-04-23 16:12:14

I have an input which allows multiple IDs. They can be entered like this:

[  1000, 1001, 1050-1060, 1100  ]              

Out of this input string I want to get all the single IDs. I already found this to split after each ,, so the part with 1000, 1001 already works.

data : itab TYPE TABLE OF string,
SPLIT l_bukrs_string AT ';' INTO TABLE itab.

My problem is the self-built range. Any idea how I could combine this with the case above to split 1050-1060 into single values?

I want to get 1050 | 1051 | 1052 | ... | 1060 out of it.

Appreciate every hint :) Thank you so much!

Questioner
RNH
Viewed
43
Legxis 2020-02-11 22:52

The easiest solution would be to use a real range/select-option for user (?) input instead. Then you would use that range to select every value from the database table.

If you cannot use a real range/select-option, then you could convert the string to one as shown below.

DATA: bukrs_string   TYPE string,
      split_bukrs    TYPE TABLE OF string,
      bukrs          TYPE bukrs,
      bukrs_between  TYPE TABLE OF bukrs,
      bukrs_range    TYPE RANGE OF bukrs,
      bukrs_rline    LIKE LINE OF bukrs_range,
      bukrs_table    TYPE TABLE OF bukrs.

FIELD-SYMBOLS: <string>     TYPE string,
               <bukrs>      TYPE bukrs,
               <bukrs_from> TYPE bukrs,
               <bukrs_to>   TYPE bukrs.

bukrs_string = '1000, 1001, 1050-1060, 1100'.
CONDENSE bukrs_string NO-GAPS.
SPLIT bukrs_string AT ',' INTO TABLE split_bukrs.

LOOP AT split_bukrs ASSIGNING <string>.

  bukrs_rline-sign = 'I'.

  IF <string> CA '-'.

    SPLIT <string> AT '-' INTO TABLE bukrs_between.
    bukrs_rline-option = 'BT'.
    READ TABLE bukrs_between INDEX 1 ASSIGNING <bukrs_from>.
    bukrs_rline-low = <bukrs_from>.
    READ TABLE bukrs_between INDEX 2 ASSIGNING <bukrs_to>.
    bukrs_rline-high = <bukrs_to>.

  ELSE.

    bukrs_rline-option = 'EQ'.
    bukrs = <string>.
    bukrs_rline-low = bukrs.

  ENDIF.

  APPEND bukrs_rline TO bukrs_range.
  CLEAR bukrs_rline.

ENDLOOP.

SELECT bukrs
  FROM t001
  INTO TABLE bukrs_table
 WHERE bukrs IN bukrs_range.

Before you split the string, you would condense it, to remove all spaces. Then you would loop over the resulting parts and check if it contains any '-'. If that is the case, you split it again and create a BETWEEN entry in your range (consider if you may want an additional check to see if the latter number is actually higher). If there is no '-', you just create an EQUAL entry.

After you have your real range, you use it to select from the database. This is because not every bukrs in that range has to exist. You may only have 1000, 1050, 1055 and 1060, for example.

Edit: The reason there is no command, function module or class to convert a range to individual values is because what needs to be done changes heavily depending on WHAT data the range is for and if/how much values need to be verified. If you have an integer range, then all you need to do is take the from-value and add 1 to it until you reach the to-value. What about a range of binary floating point numbers? What about a range of colours? What about your range of company codes, where not all of them necessarily exist? That's why the conversion has to be done manually.