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: 

Delete fixed no of leading zeroes

Former Member
0 Kudos
2,216

Hi All,

I have a field Cost code in a table in which values like C0004,0000000067, 0000000253 are maintained for a single Person.

In the final output in my report I want to display the Cost Code of a single person like this in one single column:

C0004,067, 253. (comma separated)

Pls note : The field type in table is character of length 10. If the field is numeric that is no alphabets then minimum 3 characters should be displayed.

That is if value is 0000000067 then it should be displayed as 067. And if it is 0000000253 then as 253. And alphanumeric should be displayed as it is.

On run-time how can I figure out if the field is numeric or alpha-numeric?

I used following thing like:

LV_KOSTL  = WA_HRT1018-KOSTL.

LV_KOSTL =  LV_KOSTL+7(3).

That is tried deleting 7 leading zeroes.

But it gives a dump when the value comes as alphanumeric.

Please help.

Regards,

Pranita

1 ACCEPTED SOLUTION

Former Member
0 Kudos
878

Hi,

You can also use NUMERIC_CHECK FM to check if the value is NUMC or CHAR. Based on which you can truncate the leading zeros in the string.

Hope it helps.

Regards,

Adithi

12 REPLIES 12

NagaPrakashT
Contributor
0 Kudos
878

Hi Pranita,

  You can try this way

First check if any alphabets contains in the Cost Code( i.e of type CHAR10 )

IF lv_cost_code CA 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.

   output = lv_cost_code.

ELSE.

   output = lv_cost_code+7(3).

ENDIF.

Arun_Prabhu_K
Active Contributor
0 Kudos
878

Hi Pranita,

     Make use of the check condition for your requirement.

     CO '0123456789'.

Regards.

former_member194152
Contributor
0 Kudos
878

you can use describe field statement to get data type of a varriable :

data flag type mara-matnr.
data comp type string.
DESCRIBE FIELD flag TYPE comp.
write : / comp.

0 Kudos
878

Hi Ganga,

Use this functional module

NUMERIC_CHECK

DATA : temp1(10VALUE '000012',
        htype       TYPE dd01v-datatype,
        slet        TYPE i,
        var(3).

CALL FUNCTION 'NUMERIC_CHECK'
   EXPORTING
     string_in = temp1
   IMPORTING
     htype     = htype.

IF htype = 'NUMC'.
   slet = STRLEN( temp1 ).
   slet = slet - 3.
   var = temp1+slet(3).
ENDIF.

WRITE 😕 var.

DATA : temp1(10VALUE 'C00012',
        htype       TYPE dd01v-datatype,
        slet        TYPE i,
        var(3).

CALL FUNCTION 'NUMERIC_CHECK'
   EXPORTING
     string_in = temp1
   IMPORTING
     htype     = htype.

IF htype = 'NUMC'.
   slet = STRLEN( temp1 ).
   slet = slet - 3.
   var = temp1+slet(3).
WRITE 😕 'I am numaric', var.
else.

WRITE 😕 'I am char', temp1.

ENDIF.



former_member585060
Active Contributor
0 Kudos
878

Hi,

Try this way to check whether the field contains only numerics and not alphabits.

IF lv_cost CA '0123456789' AND

    lv_cost NA 'sy-abcde.

  lv_field =  lv_cost+7(3).

ELSE.

lv_field = lv_cost.

ENDIF.

Thanks & Regards

Bala Krishna

Former Member
0 Kudos
878

Hi Pranita,

Use this below code, it works for both string variable and integer variable.

DATA:

       lv_value TYPE i VALUE '0000038',  "lv_value can be integer or char or string.

       lv_final TYPE string,

       lv_len TYPE i.

**** Move the value to string variable.

MOVE lv_value TO lv_final.

**** Useful incase of value copied from integer type

CONDENSE lv_final NO-GAPS.

**** Remove the leading zero's

SHIFT lv_final LEFT DELETING LEADING '0' .

**** Insert zero's if necessary

lv_len = strlen( lv_final ).

WHILE lv_len < 3.

   ADD 1 TO lv_len.

   CONCATENATE '0' lv_final INTO lv_final.

ENDWHILE.

Former Member
0 Kudos
879

Hi,

You can also use NUMERIC_CHECK FM to check if the value is NUMC or CHAR. Based on which you can truncate the leading zeros in the string.

Hope it helps.

Regards,

Adithi

0 Kudos
878

Thanks a lot for the responses.

I solved the problem using the FM NUMERIC_CHECK.

0 Kudos
878

One more thing.

If I need to make an FM for this that usr inputs any numeric string with any no of leading zeros.

But the result should be just 3 charaters.

0 Kudos
878

Hi Pranita,

For that see this code..

DATA : temp1(100VALUE '0000000000012',
        htype       TYPE dd01v-datatype,
        slet        TYPE i,
        var(3).

CALL FUNCTION 'NUMERIC_CHECK'
   EXPORTING
     string_in = temp1
   IMPORTING
     htype     = htype.

IF htype = 'NUMC'.
   slet = STRLEN( temp1 ).
   slet = slet - 3.
   var = temp1+slet(3).
WRITE 😕 'I am numaric', var.
else.

WRITE 😕 'I am char', temp1.

ENDIF.

0 Kudos
878

Hi Pranita,

FM CONVERSION_EXIT_ALPHA_OUTPUT is the FM to truncate the leading zeros.

Also can use:

SHIFT a LEFT DELETING LEADING '0'.


But don't think there is would any FM to truncate upto a certain value.

What if the numeric value in your query has more than 3 digits say 00123234 then truncating until 3 would not serve the purpose.

Regards,

Adithi

Former Member
0 Kudos
878

Hi,

You can use FM numeric_check to check whether the value is numeric.

CALL FUNCTION 'NUMERIC_CHECK'

         EXPORTING

              STRING_IN  = X

         IMPORTING

              STRING_OUT = X

              HTYPE      = H_TYPE.

If variable X contains a character then H_TYPE will return 'CHAR'. Check this and then you can decide to get 3 characters or whole string.

Regards,

Mahen.