SHIFT

Variants:

1. SHIFT c.

2. SHIFT c BY n PLACES.

3. SHIFT c UP TO c1.

4. SHIFT c LEFT  DELETING LEADING  c1.

5. SHIFT c RIGHT DELETING TRAILING c1.

Depending on whether byte or character string processing is carried out, the operands can only be byte-type or character-type (see Overview).

The syntax check performed in an ABAP Objects context is stricter than in other ABAP areas. See Only character fields allowed in string processing.

In some cases, the syntax rules that apply to Unicode programs are different than those for non-Unicode programs. See Character String Processing and Unicode.

Variant 1

SHIFT c.


Extras:

1. ... CIRCULAR

2. ... RIGHT

3. ... LEFT

4. ... IN BYTE MODE

5. ... IN CHARACTER MODE

Effect

Shifts the contents of c one position to the left. The first letter is discarded.
If c has the type C or X in the addition IN BYTE MODE, a trailing space is inserted.
If c has the type STRING or XSTRING in the addition IN BYTE MODE, no trailing space is inserted, so the result is one character shorter than the original string.

Example

DATA: ALPHA1(10) TYPE C VALUE 'ABCDEFGHIJ',
      ALPHA2     TYPE STRING.
ALPHA2 = ALPHA1.
SHIFT ALPHA1.
SHIFT ALPHA2.

ALPHA1 now has the contents 'BCDEFGHIJ ', ALPHA2, on the other hand, 'BCDEFGHIJ'.

Addition 1

... CIRCULAR

Effect

Shifts C cyclically, that is, the character that is pushed out is reinserted at the other end of the string. You can use this addition with both the RIGHT and LEFT additions.

Example

DATA: ALPHA1(11) TYPE C VALUE 'ABCDEFGHIJ',
      ALPHA2     TYPE STRING.
ALPHA2 = ALPHA1.
SHIFT ALPHA1 CIRCULAR.
SHIFT ALPHA2 CIRCULAR.

ALPHA1 now has the contents 'BCDEFGHIJ A' and ALPHA2 the contents 'BCDEFGHIJA'.

Addition 2

... RIGHT

Effect

Shifts the contents of c to the right instead of the left. A leading space is inserted at the left-hand end.
If c has type C and the resulting string is longer than the C field, the last character is omitted.
If c has the type STRING, the resulting string will be one character longer than the original string.

Example

DATA: ALPHA1(10) TYPE C VALUE 'ABCDEFGHIJ',
      ALPHA2(12) TYPE C,
      ALPHA3     TYPE STRING.
ALPHA3 = ALPHA2 = ALPHA1.
SHIFT ALPHA1 RIGHT.
SHIFT ALPHA2 RIGHT.
SHIFT ALPHA3 RIGHT.

ALPHA1 now has the contents ' ABCDEFGHI' , ALPHA2 the contents ' ABCDEFGHIJ ', and ALPHA3 the contents ' ABCDEFGHIJ'.

Addition 3

... LEFT

Effect

Shifts the contents of c to the left. This is the default setting (see above), and the addition is therefore optional.

Addition 4

... IN BYTE MODE

Effect

Can be used in combination with the additions RIGHT, LEFT, and CIRCULAR. The field c must be of the type X or XSTRING. Zeroes are inserted instead of spaces.

Addition 5

... IN CHARACTER MODE

Effect

This is the default setting (see above), and the addition is therefore optional.

Variant 2

SHIFT c BY n PLACES.


Extras:

As in variant 1.

Effect

Shifts the contents of c by n places to the left. This statement has the same semantics as repeating the simple SHIFT statement n times.
This variant has the same additions as described above.

Example

DATA: ALPHA1(10) TYPE C VALUE 'ABCDEFGHIJ',
      ALPHA2     TYPE STRING,
      FIVE       TYPE I  VALUE 5.
ALPHA2 = ALPHA1.
SHIFT ALPHA1 BY FIVE PLACES.
SHIFT ALPHA2 RIGHT BY 2 PLACES.

ALPHA1 now has the contents 'FGHIJ     ' and ALPHA2 the contents '  ABCDEFGHIJ'.

Note

If n is zero or negative, c remains unchanged.
If c has type C and n is greater than the length of c, c is filled with spaces after a non-circular shift.
If c has the type STRING and n is greater than the length of the string, c contains an empty string after being shifted to the left.

Variant 3

SHIFT c UP TO c1.


Extras:

As in variant 1.

Effect

Searches c for the string contained in c1 (starting from the left). If the system finds the string, it shifts the contents of c to the left until the search string appears at the left-hand edge of c.

The Return Code is set as follows:

SY-SUBRC = 0:
c1 was found in c.
SY-SUBRC = 4:
c1 was not found in c; c remains unchanged.

Example

DATA: ALPHABET(10) VALUE 'ABCDEFGHIJ',
      THREE(3)     VALUE    'DEF',
      FOUR(4)      VALUE    'DEF '.
SHIFT ALPHABET UP TO FOUR.

SY-SUBRC is now set to 4, and the field ALPHABET remains unchanged.

SHIFT ALPHABET UP TO THREE CIRCULAR.

SY-SUBRC is now set to 0 and ALPHABET has the contents 'DEFGHIJABC'.

Note

If c1 has type C, the system searches c for the string contained in c1 in its full length, including any spaces.
If c1 is an empty string, c remains unchanged and SY-SUBRC is set to 0.

Variant 4

SHIFT c LEFT DELETING LEADING c1.

Variant 5

SHIFT c RIGHT DELETING TRAILING c1.



Extras:

1. ... IN BYTE MODE

2. ... IN CHARACTER MODE

Effect

(Variant 4/variant5). Shifts the contents of c to the left or right for as long as the string begins or ends with a character contained in c1.
If c does not begin or end with a character from c1, it remains unchanged.

Example

DATA: ALPHABET(15) VALUE '     ABCDEFGHIJ',
      M1(4)        VALUE 'ABCD',
      M2(6)        VALUE 'BJJCA '.
SHIFT ALPHABET LEFT DELETING LEADING M1.

The field ALPHABET remains unchanged.

SHIFT ALPHABET LEFT DELETING LEADING SPACE.

The field ALPHABET now has the following contents:
'ABCDEFGHIJ     '.

SHIFT ALPHABET RIGHT DELETING TRAILING M2.

ALPHABET now has the following contents:
'      ABCDEFGHI'.

Addition 1

... IN BYTE MODE

Effect

If you use this addition, the fields c and c1 must be of the type X or XSTRING. Zeroes are inserted instead of blanks.

Addition 2

... IN CHARACTER MODE

Effect

This is the default setting (see above), and the addition is therefore optional.

Note

Performance:

For performance reasons, you should avoid using SHIFT in WHILE loops.
The runtime required to shift a field with length 10 by one character to the right or left requires about 5 msn (standardized microseconds). A cyclical shift requires around 7 msn. The runtime for the ... LEFT DELETING LEADING ... variant is around 3.5 msn, for ...RIGHT DELETING TRAILING... around 4.5 msn.

Related

CONCATENATE, FIND, SEARCH, SPLIT

Additional help

Shifting Field Contents