Skip to Content
author's profile photo Former Member
Former Member

Adding zeros to a character string

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.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

9 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Jul 11, 2007 at 09: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.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 11, 2007 at 09: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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 11, 2007 at 09: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!

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 11, 2007 at 09: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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 11, 2007 at 09:17 AM

    use <b>OVERLAY A WITH '00000'.</b>

    reward if useful..

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 11, 2007 at 09: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
    

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 11, 2007 at 09:27 AM

    shift A right deleting trailing space.

    overlay A with '00000'.

    Reward if useful.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 11, 2007 at 09: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>

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 11, 2007 at 03:48 PM

    Hi,

    Check this FM CONVERSION_EXIT_NUMCV_INPUT.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.