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 count the lenght occupied by a field...???

Former Member
0 Kudos
2,011

Hi All..

I am having material No's in an internal table..

How to count a particular field's length...??

Ex :

itab-mat_no = 2dfty.

mat_no's lenght is 20 char in internal table.

Now How to count that itab-mat_no is of length 5...???

Thanks in Advance...

Pavan

1 ACCEPTED SOLUTION

Former Member
0 Kudos
258

If i use STRLEN, the syntax error states that " STRLEN is unknown..the correct one is SLEN".

If i use SLEN, again " Field type i does not permit sub-field access"..

What to do now...???

10 REPLIES 10

Former Member
0 Kudos
258

data: v_length type i.

v_length = strlen( V_FIELD ).

"where V_FIELD is the filed of which you want to

calculate the length

Former Member
0 Kudos
258

Use STRLEN for this

data : length type i.

length = strlen(itab-matnr).

Points if this helps...

Former Member
0 Kudos
258

Hi Pavan,

If your data is of type char then you can calculate length by using ABAP statment <b>STRLEN</b>.

data v_length type i.

data v_matnr type mara-matnr.

v_matnr = 'abcde'.

<b>I = STRLEN( V_MATNR ).</b>

Note: Place ( after STRLEN,Otherwise it gives you syntax error.

Thanks,

Vinay

Former Member
0 Kudos
258

hi

good

if you want to know the length of the particular variable than use the length command

... LENGTH n

Effect

This addition is only allowed for the field types C, N, X, and P. The field f is created with the length n.

Addition 10

... DECIMALS n

Effect

This addition is only necessary for field type P. When you calculate using this field or output it, it has n decimal places, where n is between 0 and 14.

When you create a new program, "floating point arithmetic" is selected by default. If you deselect this attributes, the DECIMALS specification is only effective when you output the field, not when you use it in calculations. In this case, you must ensure that you have the correct number of decimal places by multiplying or dividing the field by the appropriate power of 10. (COMPUTE).

thanks

mrutyun

Former Member
0 Kudos
259

If i use STRLEN, the syntax error states that " STRLEN is unknown..the correct one is SLEN".

If i use SLEN, again " Field type i does not permit sub-field access"..

What to do now...???

0 Kudos
258

Hi Pavan,

STRLEN will gives the length of character. I dont think it doesn't give syntax error. <b>Are you give sure that you doesnt give space after STRLEN???</b>

Make sure of syntax as follows.

<b>STRLEN( V_MATNR )</b>

If it doesnt give solution to your problem, then paste code where you are getting error including declarations part.

Thanks,

Vinay

0 Kudos
258

data: v_field(6) type c value '123344'.

data: v_length type i.

v_length = strlen( V_FIELD ).

write: v_length.

<b>note:take care give the gap b/w ( and variable.</b>v_length = strlen<b>(V_FIELD).----></b>wrong

v_length = strlen<b>( V_FIELD ).-----></b>correct

0 Kudos
258

Please post your code here so that the forum can help. What is the data type of the object that you are trying to get the length of?

Sudha

0 Kudos
258

Hi Pavan,

Why you are getting that error.This code is working fine for me.

data: v_length type i,

v_field(10) value 'TESTING'.

v_length = strlen( V_FIELD ).

write : v_length.

Output : 7

Former Member
0 Kudos
258

Hi Pavan,

If you do not use Conversion Routines you will get the length as 18 always, because SAP stores the data as character of length 18.

So even if you give itab-mat_no = 2dfty,

strlen( itab-mat_no ) will be 18.

Consider this code. Just copy paste and run.

1). Using the Conversion routine

2). Without Conversion routine.

In this way.


  CONDENSE temp_matnr NO-GAPS.
  len = STRLEN( temp_matnr ).

You will come to know the difference yourselves.

The main code is as follows.


REPORT  zztest_arun_2                           .


DATA : it_mara TYPE STANDARD TABLE OF mara WITH HEADER LINE,
       temp_matnr TYPE string,
       len     TYPE i.

SELECT * FROM mara INTO TABLE it_mara UP TO 200 ROWS.


LOOP AT it_mara.

*SAP internally stores the material number as data of length 18
*so you will get the length as 18 always.
*So use conversion routine to get the user form of material number
*Then calculate its length.


  CALL FUNCTION 'CONVERSION_EXIT_MATN2_OUTPUT'
    EXPORTING
      input  = it_mara-matnr
    IMPORTING
      output = temp_matnr.

  CONDENSE temp_matnr NO-GAPS.

  len = STRLEN( temp_matnr ).

  WRITE : / it_mara-matnr,
            len.

  CLEAR : it_mara , temp_matnr.

ENDLOOP.

Regards,

Arun Sambargi.