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

STRINGS

Hi guys,

I have a requirement wherein i need to capture the first 4 characters of a field into another variable.

For eg i have a field ' ABC' whose data contents are 200704 , 200606 , 200507...etc

I need the values 2007 , 2006 , 2005 from tha above field in the work area of another internal table.

How do i go about this??

Thanks and regards,

Frank.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

5 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Nov 21, 2007 at 09:44 AM

    Hi ,

    Use offset(length)

    check the below code

    loop at itab1.

    itad2-fields = itab1-abc+0(4).

    append itab2.

    endloop.

    <b>Reward points if usefull.</b>

    Regards,

    Siva chalasani.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 21, 2007 at 09:42 AM

    Hi frank,

    field = '200712'.
    write field(4).    "will write 2007

    rgs 😉

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 21, 2007 at 09:44 AM

    i am not hardcoding the values in the field..... i am fetching the field values from a database table into an internal table.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 21, 2007 at 09:45 AM

    write ABC(4) to <variable>

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 21, 2007 at 10:06 AM

    Hi Frank,

    After fetching the data from Database table, when you are moving to internal table just offset the value for the first 4 characters and append it to internal table.

    Regards,

    Satish

    Add a comment
    10|10000 characters needed characters exceeded

    • Offsets and lengths is a bad way to program. Check out the programming concept of "Magic Numbers".

      Far better to define a structure. Assume you've got your data into a variable type n length 6, called l_per, and you're wanting to get into a variable type n length 4, called l_year.

      TYPES: BEGIN OF period type,
              yyyy TYPE n,
              mm   TYPE n,
             END OF period_type.
      
      DATA: l_per_tmp TYPE period_type. 
      
      l_per_tmp = l_per.
      l_year = l_per-yyyy.

      This may seem long winded, but it is absolutely clear what the program is doing, and no-one needs to scratch their head over what the number 4 means, nor do they need to know the internal structure of l_per to understand the program.

      Clarity is good. Ambiguity is bad. And most of the cost of a program is in maintenance, not development.

      matt

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.