Processing Subfields

You can address subfields in any statement in which non-numeric elementary ABAP types (c, d, n, t, x) or structures that do not contain internal tables occur using the following syntax:

f[+off][(len)]

By specifying an offset +off and a length (len) directly after the field name f, you can address the part of the field starting at position off+1 with length len as though it were an independent data object. In all statements, offset off and length len can be statically specified as number literals without a plus/minus sign or they can be specified dynamically using variables.

The data type and length of the string section are as follows:

Original field

Subfield

Data type

Data type

Length

c

c

len

d

n

len

n

n

len

t

n

len

x

x

len

Structure

c

len

If you do not specify the length len, you address the section of the subfield between off and the end of the field. If the offset and length combination that you specify leads to an invalid section of the field (for example, exceeding the length of the original field), a syntax or runtime error occurs. You cannot use offset and length to address a literal or a text symbol.

You should take particular care when addressing components of structures. To ensure the correct platform-specific alignment of type i and f components, they may contain filler fields, whose lengths you need to consider when calculating the correct offset. Furthermore, conversion of the internal representation of character types to UNICODE is planned. One character will no longer occupy one byte, but two or four instead. Although offset and length specifications can work for character fields (types c, d, n, t) or for hexadecimal fields (type x), incompatible changes may occur in structures containing a mixture of numeric, character, and hexadecimal fields. SAP therefore recommends that you do not use offset and length to address components of structures.

DATA time TYPE t VALUE '172545'.
WRITE time.
 WRITE / time+2(2).
 CLEAR time+2(4).
WRITE / time.

Output:

172545
25
170000

First, the minutes are selected by specifying an offset in the WRITE statement. Then, the minutes and seconds are set to their initial values by specifying an offset in the CLEAR statement.

Offset and Length Specifications in the MOVE Statement

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

MOVE f1[+off1][(len1)] TO f2[+off2][(len2)].

Or, when you use the assignment operator:

f2[+off2][(len2)] = f1[+off1][(len1)].

The contents of the part of the field f1 that begins at position off1+1 and has a length of len1 are assigned to field f2, where they overwrite the section that begins at position off2+1 and has a length of len2.

SAP recommends that you assign values with offset and length specifications only between non-numeric fields. With numeric fields, the results can be meaningless.

Subfields in the WRITE TO Statement

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

WRITE f1[+off1][(len1)] TO f2[+off2][(len2)].

The contents of the subfield of field f1 that begins at position off1+1 and has a length of len1 are assigned to field f2, where they overwrite the section that begins at position off2+1 and has a length of len2.