Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to add zeros at the start for a character field

Former Member
0 Kudos

Hi All,

I have a 10 character field whose valus is suppose 'A00987'.

How to add zeros at the starting of this field?

The output value for the above case should be 0000A00987.

I have tried using the FM 'CONVERSION_EXIT_ALPHA_INPUT'.

But it is returning the inputted value without appending zeros..

Any pointers would be of great help.

Thanks & Regards

Gowthami

Edited by: gowthami karunya on Mar 30, 2009 10:55 AM

12 REPLIES 12

Former Member
0 Kudos

Declare the data type of the length you required and use the Below FM.

If it is still not working Find the lenght of the field Using STRLEN and concatenate the missing vallues with zeros.

0 Kudos

Hi Phanindra,

Thanks for ur prompt reply.

But finding the length of the string and appending the zeros for remaining digits would be complex for my requirement. Using the FM 'CONVERSION_EXIT_ALPHA_INPUT' is not working fine.

Regards

Gowthalmi

Former Member
0 Kudos

Hi,

FM 'CONVERSION_EXIT_ALPHA_INPUT' gives based on the data type declared for the field.

Eg: data : matnr TYPE matnr value '123456'.

after calling the FM the value become 000000000000123456. since MATNR is of type CHAR with 18 length...since the value of matnr here is of 6 char length, the FM will add the 12 preceding 0's..

OR

if you dont know what type of field to be declared, just declare the field type as N with required length..then system automatically adds the preceeding 0's with equal length.

eg: data : v1(10) TYPE n value '123456'.

write : v2.

output : 0000123456.

Hope its clear!!

Regards,

Pavan

0 Kudos

Hi Pavan,

Thanks for ur reply.

This FM works fine if the value has only numeric values.

Please try with the example value I have given.

The field declared is of char10.

Regards

Gowthami

0 Kudos

Hi,

do like below

DATA : v1(10) VALUE '100987',

v2 type i.

v2 = strlen( v1 ).

v2 = 10 - v2.

SHIFT v1 right by v2 places." deleting leading space.

do v2 times.

replace space with '0' into v1.

enddo.

OR

overlay v1 with '0000000000'.

WRITE : v1.

its working fine...i have tested it..

Regards,

Pavan

Edited by: vishnu Pavan on Mar 30, 2009 11:26 AM

Former Member
0 Kudos

Hi,,

See in FM docum.

If the input is not purely numeric, it is inserted in the display field from left to right and all extra spaces are filled with blanks.

so it does like this.

Former Member
0 Kudos
data: w_char(10) type c value 'A00987',
         w_int type i.

w_int = strlen( w_char).

w_int = 10 - w_int.

shift w_char right by w_int places.

overlay w_char '0000000000'  into w_char.                  " Blank places will be filled by zeros in front

0 Kudos

Thanks for ur reply.

My question got answered.

Regards

Gowthami

Former Member
0 Kudos

The character 'A' is preventing the function to properly add leading zeroes.

Try:

SHIFT <your_variable> RIGHT DELETING TRAILING SPACE.
TRANSLATE <your_variable> USING '  0'.

Former Member
0 Kudos

hi,

use CONVERSION_EXIT_ALPHA_INPUT -


It is used to add the Zero at the begining of the value. CONVERSION_EXIT_ALPHA_OUTPUT --- It is used to Remove Zero from the Begining of the value.

0 Kudos

>

> hi,

> use CONVERSION_EXIT_ALPHA_INPUT -


It is used to add the Zero at the begining of the value. CONVERSION_EXIT_ALPHA_OUTPUT --- It is used to Remove Zero from the Begining of the value.

Unless i'm imagining things, but the original poster already stated he used that function and it didn't work.

Former Member
0 Kudos

HI

TRY THIS CODE..

LOOP AT <FIELD NAME>.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

EXPORTING

INPUT = FIELD NAME-LOW

IMPORTING

OUTPUT = FIELD NAME-LOW.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

EXPORTING

INPUT = FIELD NAME-HIGH

IMPORTING

OUTPUT = FIELD NAME-HIGH.

MODIFY ADVCNO INDEX SY-TABIX TRANSPORTING LOW HIGH .

ENDLOOP.

THANKS

ABHI