Add Leading Zero ABAP – Zeros Padding in SAP

Add Leading Zero ABAP is a common requirement in the daily task of ABAP developper.
How to handle leading and padding zeros for SAP Data and variables in SAP using ABAP with common case of SAP Material Number

Check also Conversion ABAP: between Binary, String, XString and Table

Add Leading Zero ABAP

In order to add Leading zeros in ABAP, in general use the standard function CONVERSION_EXIT_ALPHA_INPUT.
This function will add the leading zeros until filling the length of the output. It will check the Data Type and domain in order to calculate how many zeros will be added.

SAP Material Add Leading Zeros ABAP

For Material, I recommend using an other function: CONVERSION_EXIT_MATN1_INPUT
In one project I have been working on, in some systems, SAP Material Numbers are stored in SAP Material Master data with Leading zeros and for some others systems, the SAP Material Number is handled without leading zeros in SAP.

In opposite, to Remove the leading Zeros in SAP Material Number use rather CONVERSION_EXIT_MATN1_OUTPUT.

These both Conversion routines are set in MATNR domain.

Check also the The Main Important SAP Material Master Tables ( Data & Customizing ).

Add Leading Zeros in ABAP

There are some other ways to add the leading zeros in ABAP Variable.

ABAP Unpack Statement

ABAP Statement Unpack is the easiest way to deal with zeros padding in ABAP. It can be a serious alternative for the conversion_exit_alpha_input.

ABAP Default Casting Or Write

ABAP can handle formatting SAP variables when copying variables. It will add the leading zeros for example if you copy a string variable containing a number to an integer. The Integer variable will have the needed zeros.

Other Zeros Padding Alternative in ABAP

The ABAP WRITE statement has multiple functionnalities within the formatting tools.

  • To Add leading zeros: WRITE lv_variable USING EDIT MASK ‘==ALPHA’.
  • To Remove Leading zeros: WRITE lv_variable no NO-ZERO.

SAP and SAP logo are registered trademarks of SAP AG. Sap4tech.net is not associated with SAP AG.

We have made every effort to make sure provide relevant search results, use the content on this site at your own risk.

We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to Amazon.com and affiliated sites.