SAP OFFSET LENGTH



Get Example source ABAP code based on a different SAP table
  


• + ABAP_OFFLEN
• ( ) ABAP_OFFLEN

Offset/Length Specifications for Substring Access
In operand positions, it is possible to access subareas of certain data objects by specifying an offset and a length:

ABAP_SYNTAX
dobj$[+off$]$[(len)$]
<(><)>$[+off$]$[(len)$]
dref->*$[+off$]$[(len)$]

What does it do?
An offset off can be appended directly with the character + to the identifier dobj of a data object, to <(><)> of a field symbol, or to a dereferenced data reference variable dref->*. A length len can be appended to the offset specification directly in parentheses ( ) or directly to the identifier. Offsets or lengths can be specified for:
Elementary byte-like data objects
Elementary character-like data objects
Flat structures where one of the following prerequisites must be met:
The structure only contains flat character-like components.
The first fragment of the structural fragment view is flat and character-like, and the substring addressed by specifying the offset and length is located within this fragment.
The segment of the data object is used that has the offset specified in off and the length specified in len in characters or bytes . No memory area outside the field boundaries can be addressed, except for in the statement ASSIGN. In the case of an offset specification without a length, the entire substring is addressed from off characters; in the case of a length specification without an offset, the first len characters are addressed (different rules apply to the statement ASSIGN).
The operands off and len expect data objects of the type i, which must contain positive integers, with the following exceptions:
The length 0 can be specified for strings.
A negative offset, but never length 0 , can be specified if an <(><)> field symbol is specified in the statement ASSIGN for dobj.
If off is smaller than the length of dobj, an asterisk ( *) can be entered for len. The upper limit of dobj then determines the upper limit of the memory area.
If the prerequisites are not met or if the substring defined by off and len is not fully included in the data object, except in the case of ASSIGN, a syntax error occurs if statically identifiable. Otherwise, an exception of the class CX_SY_RANGE_OUT_OF_BOUNDS is raised. If off is specified as a numeric literal, it cannot be preceded by a plus/minus sign.
The offset and length specifications are counted in characters for character-like data objects and otherwise in bytes.
A substring specified by an offset or length specification is handled like a data object of the specified length. The data type depends on the data type of the original data object, the field symbol, or the data reference variable, as shown below: Original Data TypeData Type of Substring cc nn dn tn stringstring xx xstringxstring Structure typec
In a substring access, if the length of the substring matches the length of the structure to a structure exactly, the substring does not have data type c but is handled like the structure itself.
The following restrictions apply:
At write positions, only flat data objects are allowed, that is, write access to substrings of strings is not possible.
Access to substrings of strings is also not possible in the following read positions:
Specification of dobj$[+off$]$[(len)$] as a memory area mem_area of the statement ASSIGN.
Specification of dobj$[+off$]$[(len)$] as an argument of the statement GET REFERENCE or of the reference operator REF.
Specification of dobj$[+off$]$[(len)$] as an actual parameter for input parameters when calling methods, function modules
BEGIN_SECTION VERSION 5 OUT , and subroutines
END_SECTION VERSION 5 OUT .
Offsets and lengths cannot be specified for literals or text symbols.
An offset/length specification cannot be attached directly to a table expression itab[ ... ], but to a chaining whose last position is a suitable structure component after a structure component selector.
No offset or length can be specified for an enumerated object with an enumerated type, regardless of the base type.
In case of dynamically specified operands in parentheses, no length specification can be made.
A dereferenced data reference variable dref->* must be typed completely.
For a writable expression, no offsets or lengths can be specified as memory areas of the statement ASSIGN or as arguments of the reference operator REF (table expressions only).
The following applies when assigning data objects of fixed length with offset/length specifications to an inline declaration with the declaration operator DATA or FINAL:
If the offset/length specification is applied to a data object that is directly assigned to the inline declaration, off and len must be literals or constants. Variables are not allowed.
If the offset/length specification is applied to a data object that is the result of chaining that involves a constructor expression or a table expression, the data type of the declared data object is the data type of the assigned data object in its full length and the substring is assigned according to the respective conversion rules.



Latest notes:

For reads on substrings of character-like data objects, built-in substring functions that allow searches for substrings and offsets/lengths are also available. The substring functions can also be used to process arguments where offsets and lengths cannot be specified, such as data objects with an enumerated type because there is an implicit conversion to string.
It is recommended that offsets with the value 0 are always specified explicitly, that is, dobj+0(len) instead of dobj(len) to differentiate substring access in the source code clearly from other language constructs that also contain parentheses, such as dynamic specifications, method calls, or inline declarations.
A specification of dobj+0(*), dobj+0, or dobj(*) is always interpreted as dobj. In this case, dobj can also be a data object where substring access according to the rules above is not possible.
The above restriction regarding offset/length specifications for results of chainings involving table or constructor expressions is not valid for types that are inferred at runtime, e.g. when passing to generic parameters of procedures. Then the data type of the substring is used.
No substring access cnt(len), sum(len) can be performed on data objects named cnt and sum unless an offset is specified explicitly.
NON_V5_HINTS
The compiler always interprets such an entry as a number or sum of a field len in the group level processing of an extract dataset.
Obsolete syntax forms can still occur outside of classes in connection with substring accesses.
The statement MOVE PERCENTAGE represents an obsolete form of substring access.
ABAP_HINT_END

ABAP_EXAMPLE_VX5
Offset/length specifications after a FIND statement.
ABEXA 01607
ABAP_EXAMPLE_END

ABAP_EXAMPLE_VX5
The following structure has character-like and non-character-like components:
ABEXA 01181
The fragment view splits the structure into five areas, F1 - F5.
[ aaa | bbbb | cccccccc | ddd | AAA | eeee | fffffffffffff | gg ]
[ F1 | F2 | F3 | F4 | F5 ]
Offset and length accesses are only possible on the character-like initial fragment F1 only, for example struc(21) or struc+7(14). An access such as struc+57(2) is not allowed.
ABAP_EXAMPLE_END

ABAP_EXAMPLE_ABEXA
Offset/Length Specifications
ABAP_EXAMPLE_END