STRING PROCESSING Using OFFSET
STRING PROCESSING Using OFFSET
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,
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.
======================
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
WRITE TIME.
WRITE / TIME+2(2).
CLEAR TIME+2(4).
WRITE / TIME.
172545
25
170000
F2+6(5) = F1+3(5).
CLEAR F2.
MOVE F1 TO F2+O(L). WRITE / F2.
MOVE F1+O(L) TO F2+O(L). WRITE / F2.
ABCDEFGH
CDEF
CDABCD
ABCD
CDEF
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.
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.