Jul 6, 2026

Replace Some Characters in the Middle of Text.

Requirement:

To replace the 5th character in a 10-character string (or any string), use the ABAP overlay statement or the replace section command. In ABAP, character offsets are zero-based, meaning the 5th character is at offset

Solution:

Here are the two best approaches for doing this:

Method 1: Using OVERLAY (Best for replacing specific offsets)
abap
DATA: gv_string TYPE string VALUE '1234567890'.

" The 5th character is at offset 4 (length 1). Replace it with 'X'
OVERLAY gv_string WITH '    X' ONLY ' '.

Method 2: Using REPLACE SECTION (Best for explicit length)
abap
DATA: gv_string TYPE string VALUE '1234567890'.

REPLACE SECTION OFFSET 4 LENGTH 1 OF gv_string WITH 'X'.

Method 3: Inline Expression (ABAP 7.40+)
abap
DATA(gv_string) = '1234567890'.

gv_string = |{ gv_string(4) }X{ gv_string+5 }|.

Output for all methods: 1234X67890

Jul 3, 2026

A Aample Program Using BAPI_RESERVATION_CHANGE to Change Quantity and Storage Location

Storage location changes via this BAPI aren't as clean as quantity changes — SAP requires you to also populate MOVE_ITEM correctly or the storage location update silently gets ignored while quantity still updates fine. That's the part people usually miss and then spend an hour debugging why the reservation shows on the wrong bin/plant.

BAPI_RESERVATION_CHANGE requires you to explicitly flag every field you're changing in ITEMCONTROL — omitting the flag means the value in ITEMDATA is ignored even if populated.

For storage location changes specifically, you need MOVEMENT_ALLOWED context (goods movement not yet posted against that item) — if GI already happened against the reservation item, the BAPI will reject the storage location change via RETURN.

abap
REPORT zbapi_reservation_change.

DATA: lv_rsnum   TYPE rsnum VALUE '0000001234',  " your reservation number
      lv_rspos   TYPE rspos VALUE '0001'.        " reservation item number

DATA: lt_itemdata    TYPE STANDARD TABLE OF bapi2093_res_item,
      lt_itemcontrol TYPE STANDARD TABLE OF bapi2093_res_item_ctrl,
      lt_return      TYPE STANDARD TABLE OF bapiret2.

DATA: ls_itemdata    TYPE bapi2093_res_item,
      ls_itemcontrol TYPE bapi2093_res_item_ctrl.

* --- Optional but recommended: read current item first ---
DATA ls_resb TYPE resb.
SELECT SINGLE * FROM resb
  INTO ls_resb
  WHERE rsnum = lv_rsnum
    AND rspos = lv_rspos.

IF sy-subrc <> 0.
  WRITE: / 'Reservation item not found'.
  RETURN.
ENDIF.

* --- Fill ITEMDATA: the new values ---
ls_itemdata-res_item  = lv_rspos.
ls_itemdata-material   = ls_resb-matnr.
ls_itemdata-plant      = ls_resb-werks.
ls_itemdata-stge_loc   = 'STG2'.        " new storage location
ls_itemdata-entry_qnt  = '50'.          " new quantity
ls_itemdata-entry_uom  = ls_resb-meins.
APPEND ls_itemdata TO lt_itemdata.

* --- Fill ITEMCONTROL: flags telling SAP which fields to actually apply ---
ls_itemcontrol-res_item = lv_rspos.
ls_itemcontrol-stge_loc = abap_true.    " flag: storage location changed
ls_itemcontrol-entry_qnt = abap_true.   " flag: quantity changed
APPEND ls_itemcontrol TO lt_itemcontrol.

* --- Call the BAPI ---
CALL FUNCTION 'BAPI_RESERVATION_CHANGE'
  EXPORTING
    reservation = lv_rsnum
  TABLES
    itemdata    = lt_itemdata
    itemcontrol = lt_itemcontrol
    return      = lt_return.

* --- Check for errors before committing ---
READ TABLE lt_return TRANSPORTING NO FIELDS
  WITH KEY type = 'E'.
IF sy-subrc = 0.
  LOOP AT lt_return INTO DATA(ls_return) WHERE type CA 'EA'.
    WRITE: / ls_return-message.
  ENDLOOP.
ELSE.
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      wait = abap_true.
  WRITE: / 'Reservation updated successfully'.
ENDIF.