2007 Jul 11 10:14 AM
Hello friends,
I want to add leading zeros for a field.
the field is a character string.
for example ,
data: A(5) type c.
now when A = 'ab' (non-numeric value)
i want this to be converted in '000ab'
so, is there any standard Function module or any other way for doing this conversion ?
I tried the FM 'CONVERSION_EXIT_ALPHA_INPUT' but this FM does not work for non-numeric inputs..
Thanks.
2007 Jul 11 10:24 AM
this will work
REPORT ychatest.
DATA : v_char(5) TYPE c VALUE 'ab'.
SHIFT v_char RIGHT deleting trailing space.
translate v_char using ' 0'.
WRITE : v_char.
2007 Jul 11 10:16 AM
Hi,
Do it like this,
data: A(5) type c.
data: b(5) type n.
a = 'AB'
unpack A to B.
Reward if useful!
2023 Mar 22 12:27 PM
2007 Jul 11 10:17 AM
2007 Jul 11 10:21 AM
Hi,
The packed field is transported right-justified to the character field, if required with a
decimal point. The first position is reserved for the sign. Leading zeros appear as
blanks. If the target field is too short, the sign is omitted for positive numbers. If this is still not sufficient, the field is truncated on the left. ABAP indicates the truncation with an asterisk (*). If you want the leading zeros to appear in the character field, use UNPACK instead of MOVE.
UNPACK
Converts variables from type P to type C.
Syntax
UNPACK <f> TO <g>.
Unpacks the packed field <f> and places it in the string <g> with leading zeros. The opposite of PACK.
Regards,
Bhaskar
2013 Apr 17 7:29 AM
I just measured the different alternatives and unpack is clearly the fastest.
2007 Jul 11 10:24 AM
this will work
REPORT ychatest.
DATA : v_char(5) TYPE c VALUE 'ab'.
SHIFT v_char RIGHT deleting trailing space.
translate v_char using ' 0'.
WRITE : v_char.
2016 Aug 24 7:17 AM
2022 Jul 06 11:36 AM
Yes, It works.
SHIFT v_char RIGHT deleting trailing space.
translate v_char using ' 0'.
2007 Jul 11 10:25 AM
i think you cant add zero for non numeric value by std fn modules.
do like this.
data : a(5) value 'ab',
len type i.
compute len = strlen( text ).
len = 5 - len."since you have declared a(5)
do len times.
concatenate '0' a into a.
enddo.
regards
shiba dutta
2007 Jul 11 10:26 AM
Hello,
Check this code. It's working for me.
DATA: char TYPE char5.
DATA: strglen TYPE i.
DATA: len TYPE i.
char = 'AB'.
len = STRLEN( char ).
strglen = 5 - len.
DO strglen TIMES.
CONCATENATE '0' char INTO char.
ENDDO.
WRITE:/ char.
Regards,
Deepu.K
2007 Jul 11 10:27 AM
shift A right deleting trailing space.
overlay A with '00000'.
Reward if useful.
2007 Jul 11 10:28 AM
modified..
DATA: a(5) TYPE c.
<b>data: len1 type i,
len2 type i.
a = 'AB'.
len1 = strlen( a ).
len2 = 5 - len1.
move a0(len1) to alen2(len1).
clear a+0(len1).
overlay a with '00000'.
</b>
2007 Jul 11 4:48 PM
2024 Jul 19 12:02 AM - edited 2024 Jul 19 12:03 AM
The best way to do this, per the coding queen Jelena is with a string template:
DATA: lv_n_matnr TYPE matnr VALUE '517352'.
"Later:
DATA(lv_zp_matnr) = | { lv_n_matnr ALPHA = IN } | .
This takes care of the "shift right" and the zero padding in one step.
Test ABAP output: