Substring in SAP's ABAP programming

Unlike many of the higher level languages there is no stand-alone substring call. We have split, shift, replace, translate and condense but no substring. So if we want to know the first four characters from the start of a field how do we do it?

The WRITE statement is what we use to substring a field. The syntax is as follows:

WRITE fieldname+starting_position(field_length) to variable

The fieldname is the source (or input). The plus sign precedes the starting position of the substring. IMPORTANT The traditional method of counting on computers is followed. The first position in the string is 0. Immediately after (no space) comes the length of the substring you are going to use. You can also substring the variable that you are writing the string to (the target). I have included an example that illustrates substringing on the source and target fields.

A real-life use of the substring functions

I needed to build a character date field for a cross-platform integration project I was working on. First, I defined the date field:

data: datechar(10).

To build the date lets use sy-datum, which has the current system date.

  write SY-DATUM+4(2) to datechar+0(2).
  write '/' datechar+2(1).
  write SY-DATUM+6(2) to datechar+3(2).
  write '/' TO datechar+5(1).
  write SY-DATUM+0(4) to datechar+6(4).

Value of SY-DATUM 20010616

Here's how the field changes with the execution of each line.

This is just one of the great functionalities of WRITE.

Return to ABAP help

[Our portfolio] [A history of Computers and Networks]
[Perl help] [ABAP help] [MySQL help]
[TCP/IP troubleshooting] [HTML help] [Feedback] [Humor]
Advertise on Golden Ink's Georgia Network

In Association with