Add Leading Zero ABAP – Zeros Padding in SAP

ERP

Add Leading Zero ABAP is a common requirement in the daily task of ABAP developer.

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 zeros in ABAP, in general use the standard function CONVERSION_EXIT_ALPHA_INPUT.

This function will add the leading zeros until filling the length with the output. It will check the Data Type and domain in order to calculate how many zeros will be added.

Add Leading Zero ABAP - Zeros Padding in SAP

SAP Material Add Leading Zeros ABAP

For Material, I recommend using another 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.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
  EXPORTING
    INPUT  = WA_MATNR
  IMPORTING
    OUTPUT = WA_MATNR.

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

These both Conversion routines are set in MATNR domain.

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

Add Leading Zeros in ABAP

There are some other ways to add the 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.

DATA : input(16),
output(6) TYPE c VALUE '123456'.
unpack output to input.
write /: v1 , v2

Read also SAP Alternative Unit of Measure for Material.

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.

Data: Lv_Char(10) type c value '12345'.
Data: lv_num(10) type n.

lv_num = lv_char. 
" lv_num = '0000012345'

Other Zeros Padding Alternative in ABAP

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

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

For more complete ABAP Tutorials check the following book Mastering SAP ABAP: A complete guide to developing fast, durable, and maintainable ABAP programs in SAP