Adding leading zeros ABAP in SAP easy and important task. I will show examples how to add leading 0 in ABAP with 3 examples. There are more then one way how we can add zeros in ABAP but all have differences the choice is yours.
Add leading 0 ABAP with CONVERSION_EXIT_ALPHA_INPUT
For adding leading zeros ABAP we can use function module CONVERSION_EXIT_ALPHA_INPUT. Parameters function module CONVERSION_EXIT_ALPHA_INPUT ABAP:

Example using function module CONVERSION_EXIT_ALPHA_INPUT
Using CONVERSION_EXIT_ALPHA_INPUT in ABAP program for add leading zeros in partner number SAP:
1 2 3 4 5 6 7 |
DATA(lv_partner) = CONV bu_partner( '1' ). CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' EXPORTING input = lv_partner IMPORTING output = lv_partner. " result lv_partner = '0000000001'. |
ADDITION: If you send to function module not number for example: ‘HR0001’ (in SAP CRM in this field also there are and numbers with ‘HR’, for example). As result function module back to you the same value without changing and without dumps.
How add leading zeros ABAP with New syntax ABAP
Add leading zeros in ABAP with new ABAP syntax. We have good opportunities for using new ABAP constructions in adding zeros task. Using ALPHA ABAP more short and useful then FM CONVERSION_EXIT_ALPHA_INPUT.
1 2 3 |
DATA(lv_partner) = CONV bu_partner( '1' ). lv_partner = |{ lv_partner ALPHA = IN }|. " result lv_partner = '0000000001'. |
ADDITION: If you send to this function not number, for example, like this: ‘HR0001’ (in SAP CRM in this field also there are and numbers with ‘HR’, for example) . As result function back to you the same value without changing and without dumps.
How to add leading zero ABAP with UNPACK
Using UNPACK ABAP if you work in new ABAP syntax it is not advisable in my opinion, but you need to know how this construction work for reading ABAP code.
1 2 3 |
DATA(lv_partner) = CONV bu_partner( '1' ). UNPACK lv_partner TO lv_partner. " result lv_partner = '0000000001'. |
ADDITION: In typical examples with numbers all will be the same like in previous examples. But if you try to send not number ( ‘HR0001’ for example ) to UNPACK as result you will get dump with ‘CX_SY_CONVERSION_NO_NUMBER’.
How to remove or delete leading 0 in ABAP you can find on this page: