0% found this document useful (0 votes)
4K views

STRING PROCESSING Using OFFSET

1) Offset is used in ABAP to extract parts of a string starting from a specified position for a given length. For example, to extract the month from a date string in SY-DATUM, you can use SY-DATUM+4(2). 2) The offset and length specifications in ABAP statements like WRITE can be numeric variables, allowing dynamic extraction of string portions. 3) Common uses of offset include parsing date/time fields, combining strings, and justification in output.

Uploaded by

mnsk26
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4K views

STRING PROCESSING Using OFFSET

1) Offset is used in ABAP to extract parts of a string starting from a specified position for a given length. For example, to extract the month from a date string in SY-DATUM, you can use SY-DATUM+4(2). 2) The offset and length specifications in ABAP statements like WRITE can be numeric variables, allowing dynamic extraction of string portions. 3) Common uses of offset include parsing date/time fields, combining strings, and justification in output.

Uploaded by

mnsk26
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

STRING PROCESSING using OFFSET

Offset Concept in SAP ABAP


Most of the time in ABAP, we may have to read the part of data of a field (say X) and populate to
other field (Say Y). In this case we have to use the offset concept to read the value.

Example:

SY-DATUM is the standard SAP field, which holds the current system date. The format of the date
in SY-DATUM field will be YYYYMMDD.

Where,

 YYYY => Year


 MM => Month
 DD => Day

In most of the cases we may have to read the value of month form the SY-SATUM and pass to some
variable. Similarly we may have to read the value of year & day.

Below is a simple SAP ABAP program which gives clear picture on how to use offset concept.

======================

REPORT  ZVTEST_PGM_1.

DATA: l_day(2)   TYPE c, " Day
      l_month(2) TYPE c, " Month
      l_year(4)  TYPE c. " Year

*--Print the value of Current System Date (YYYYMMDD)
WRITE:/ 'Current System Date : ' , sy-datum.

*--Use offset concept & populate the Year, Month & Day value to Local valriables

*--Year: Starting form Zeroth Position next 4 characters
l_year = sy-datum+0(4).

*--Month: Starting form fourth Position next 2 characters
l_month = sy-datum+4(2).

*--Day: Starting form sixth Position next 2 characters
l_day = sy-datum+6(2).

*--Print value of Year, Month & date which is stored in local variables
WRITE:/ 'Year  : ', l_year.
WRITE:/ 'Month : ', l_month.
WRITE:/ 'Day   : ', l_day.

======================

OFFSET STRING PROCESSING


Offset is used to address a section of string according to desire way. For e.g. we have a string
say v_num containing 10 character “1234567890” and we want that the from left leaving 1&2
rest other digit should be zeros, means we want output like “1200000000”. For such output we
use offset method to get the desired string.
Syntax: Var+Offset(Length) The offset and length of the target field must be numeric literals
without a preceding sign.
Code snippet:
REPORT  ZOFFSET_TEST.

data: v_num(10) TYPE n VALUE '1234567890',
CLEAR v_num+2(8).
WRITE / v_num.

Output

       
Let’s take one more example to explain offset. Say suppose we have two string one containing
numbers let it be as v_num “1234567890” and other be v_ch “ABCDEFGHIJ”. Now we want a
combination i.e. alpha numeric string as “ABCDEF7890” where last four character be from
v_num last four digit.
Code snippet:
REPORT  ZOFFSET_TEST.

data: v_num(10) TYPE n VALUE '1234567890',
      v_ch(10) VALUE 'ABCDEFGHIJ'.
v_ch+6(4) = v_num+6(4).
WRITE / v_ch.

Output
         
Offset and Length Specifications in the WRITE TO Statement
In the WRITE TO statement, the offset and length specifications of the target field can be
variables. The offset and length of the target field must be numeric literals without a preceding
sign. For example let’s see the below code.
Code snippet:
REPORT  ZOFFSET_TEST.

DATA: v_num(10) TYPE n VALUE '1234567890',
      string(20) TYPE n,
      offset TYPE i VALUE 4,
      length TYPE i VALUE 6.
WRITE v_num to string+offset(length).
WRITE string.
Output

              

DATA TIME TYPE T VALUE '172545'.

WRITE TIME.
WRITE / TIME+2(2).
CLEAR TIME+2(4).
WRITE / TIME.

The output appears as follows:

172545

25

170000

DATA: F1(8)  VALUE 'ABCDEFGH',


      F2(20) VALUE '12345678901234567890'.

F2+6(5) = F1+3(5).

In this example, the assignment operator functions as follows:


DATA: F1(8) VALUE 'ABCDEFGH',
F2(8).

DATA: O TYPE I VALUE 2,


L TYPE I VALUE 4.

MOVE F1 TO F2.      WRITE F2.


MOVE F1+O(L) TO F2.      WRITE / F2.
MOVE F1 TO F2+O(L). WRITE / F2.

CLEAR F2.
MOVE F1 TO F2+O(L). WRITE / F2.
MOVE F1+O(L) TO F2+O(L). WRITE / F2.

This produces the following output:

ABCDEFGH

CDEF

CDABCD

  ABCD

  CDEF

Offset and Length Specifications in the WRITE TO Statement


For the WRITE TO statement, the syntax for specifying offset and length is as follows:

WRITE <f1>[+<o1>][(<l1>)] TO <f2>[+<o2>][(<l2>)].

The contents of the part of the field <f1> which begins at position <o1>+1 and has a length of <l1> are
converted to a character field and assigned to field <f2>, where they overwrite the section which
begins at position <o2>+1 and has a length of <l2>.

In the WRITE TO statement, the offset and length specifications of the target field can be variables.
The offset and length of the target field must be numeric literals without a preceding sign.
DATA: STRING(20),
NUMBER(8) TYPE C VALUE '123456',
OFFSET TYPE I VALUE 8,
LENGTH TYPE I VALUE 12.

WRITE NUMBER(6) TO STRING+OFFSET(LENGTH) LEFT-JUSTIFIED.


WRITE: / STRING.
CLEAR STRING.

WRITE NUMBER(6) TO STRING+OFFSET(LENGTH) CENTERED.


WRITE: / STRING.
CLEAR STRING.

WRITE NUMBER TO STRING+OFFSET(LENGTH) RIGHT-JUSTIFIED.


WRITE: / STRING.
CLEAR STRING.

This produces the following output:

        123456

           123456

              123456

The first six characters of the field NUMBER are written left-justified, centered, and
right-justified into the last 12 characters of the field STRING.

You might also like