Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Split a field

Former Member
0 Likes
3,393

Hi all,

I have a field of length 8. I need to split it into components of different lengths. How would i do it in ABAP?

Bala

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
2,079

Are they fixed lengths? You can use offsets.

data: char(8) type c.
data: val1(6) type c.
data: val2(2) type c.

val1 = char+0(6)  .
val2 = char+6(2)  .

Regards,

Rich Heilman

9 REPLIES 9
Read only

Former Member
0 Likes
2,079

hello,

you have to use the split command.

SPLIT f AT g INTO h1 ... hn.

DATA: NAMES(30) TYPE C VALUE 'Charly, John , Peter',

NAMES2 TYPE STRING,

ONE(10) TYPE C,

TWO(10) TYPE C,

THREE TYPE STRING,

FOUR(4) TYPE C VALUE 'FOUR',

DELIMITER(2) VALUE ','.

SPLIT NAMES AT DELIMITER INTO ONE TWO.

  • ONE contains 'Charly' and TWO contains 'John , Pet'.

  • SY-SUBRC is 4, because TWO was not large enough to

  • accommodate the whole of the remaining string

SPLIT NAMES AT ',' INTO ONE TWO THREE.

  • ONE contains 'Charly', TWO contains ' John',

  • THREE contains ' Peter'.

SPLIT NAMES AT ', ' INTO ONE THREE TWO.

  • ONE contains 'Charly', THREE contains 'John',

  • TWO contains 'Peter'.

CONCATENATE NAMES '' INTO NAMES2 SEPARATED BY SPACE.

SPLIT NAMES2 AT DELIMITER INTO ONE TWO THREE FOUR.

  • ONE contains 'Charly', TWO contains 'John',

  • THREE contains 'Peter ', FOUR is empty.

SPLIT NAMES2 AT DELIMITER INTO ONE FOUR THREE.

  • ONE contains 'Charly', FOUR contains 'John',

  • THREE contains 'Peter', SY-SUBRC is 4, since

  • FOUR was not large enough (spaces are significant

  • characters!)

Reward if helps.

Thanks,

krishna

Message was edited by: Krishnakumar

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
2,080

Are they fixed lengths? You can use offsets.

data: char(8) type c.
data: val1(6) type c.
data: val2(2) type c.

val1 = char+0(6)  .
val2 = char+6(2)  .

Regards,

Rich Heilman

Read only

Former Member
0 Likes
2,079

say if u want to split it into 2 parts

say string = 'sdn/sap'.

split string at '/' into str1 str2.

Read only

Former Member
0 Likes
2,079

use SPLIT

or use OFFSET

v_line(8) type c value 'ABCDEFGH'.

v1 = v_line+0(2). " first 2 chars will be moved to v1

V2 = V_LINE+2(3). "NEXT 3 CHARS WILL BE V2

regards

srikanth.

Message was edited by: Srikanth Kidambi

Message was edited by: Srikanth Kidambi

Read only

Former Member
0 Likes
2,079

Hi,

U can use offset,

example

lv_data ............of length 8.

lv_temp = lv_data+starting_location(Lenght).

eg:-

lv_temp = lv_data+0(2).

first 2 char will be copied ( 1st and 2nd )

lv_temp = lv_data+2(2).

3rd and 4th data will be copied

lv_temp = lv_data+4(2).

5th and 6th data will be copied

and so on

<b>reward points & mark helpful answers</b>

Read only

Former Member
0 Likes
2,079

Hi Bala,

Try with this code to display different lengths of string.This will lists all possible combination of strings.

DATA V_NAME TYPE STRING VALUE 'SPILTFIELD'.

DATA V_LENGTH TYPE I.

DATA V_INDEX1 TYPE SYINDEX.

DATA V_INDEX2 TYPE SYINDEX.

V_LENGTH = STRLEN( V_NAME ).

DO V_LENGTH TIMES.

V_INDEX1 = SY-INDEX.

DO V_LENGTH TIMES.

V_INDEX2 = SY-INDEX.

WRITE V_NAME+V_INDEX1(V_INDEX2).

ENDDO.

ENDDO.

Thanks,

Vinay

Read only

0 Likes
2,079

<b>FYI</b>

SPLIT f AT g INTO h1 ... hn.

Extras:

1. ... IN BYTE MODE

2. ... IN CHARACTER MODE

Effect

Splits f wherever the separator g occurs and places the resulting sections into the fields h1 ... hn (n >= 2). Note that if g has type C, the field is used in its defined and not its occupied length.

The field is split using the following procedure: f is split internally into a set of target fields k1 to kn with the same type as f. These are then transferred into the actual target fields h1 to hn using MOVE semantics.

The Return Code is set as follows:

SY-SUBRC = 0:

All of the fields hi (1 <= i <= n) were large enough.

SY-SUBRC = 4:

One of the fields hi was not large enough and significant characters were lost.

Examples

DATA: NAMES(30) TYPE C VALUE 'Charly, John , Peter',

NAMES2 TYPE STRING,

ONE(10) TYPE C,

TWO(10) TYPE C,

THREE TYPE STRING,

FOUR(4) TYPE C VALUE 'FOUR',

DELIMITER(2) VALUE ','.

SPLIT NAMES AT DELIMITER INTO ONE TWO.

  • ONE contains 'Charly' and TWO contains 'John , Pet'.

  • SY-SUBRC is 4, because TWO was not large enough to

  • accommodate the whole of the remaining string

SPLIT NAMES AT ',' INTO ONE TWO THREE.

  • ONE contains 'Charly', TWO contains ' John',

  • THREE contains ' Peter'.

SPLIT NAMES AT ', ' INTO ONE THREE TWO.

  • ONE contains 'Charly', THREE contains 'John',

  • TWO contains 'Peter'.

CONCATENATE NAMES '' INTO NAMES2 SEPARATED BY SPACE.

SPLIT NAMES2 AT DELIMITER INTO ONE TWO THREE FOUR.

  • ONE contains 'Charly', TWO contains 'John',

  • THREE contains 'Peter ', FOUR is empty.

SPLIT NAMES2 AT DELIMITER INTO ONE FOUR THREE.

  • ONE contains 'Charly', FOUR contains 'John',

  • THREE contains 'Peter', SY-SUBRC is 4, since

  • FOUR was not large enough (spaces are significant

  • characters!)

Addition 1

... IN BYTE MODE

Effect

If you use this addition, all fields must be of the type X or XSTRING.

Addition 2

... IN CHARACTER MODE

Effect

This addition is optional and corresponds to the default setting (see above).

Notes

If the number of target fields is not greater than the number of separator strings in the source string, very little information can be lost. In this case, the last target field contains the "remainder", including the separator string (see first example).

If the source field does not contain the separator string, or if you specify an empty C string as the separator, the entire source field is placed in the first target field.

Any target fields that are not required are deleted.

If f begins with the separator string g, the first target field (h1) contains an initial value.

Variant 2

SPLIT f AT g INTO TABLE itab.

Extras:

As with variant 1

Effect

Similar to variant 1

The sections of f are placed in the internal table itab. The sytsem creates a table row for each section of f.

Note: If f ends with the separator string g, the system does not create an empty table row at the end. This is in contrast to what happens when g occurs at the beginning of f.

Example

TYPES: BEGIN OF ITAB_TYPE,

WORD(20),

END OF ITAB_TYPE.

DATA: ITAB TYPE STANDARD TABLE OF ITAB_TYPE WITH

NON-UNIQUE DEFAULT KEY INITIAL SIZE 5.

SPLIT 'STOP Two STOP Three STOP ' AT 'STOP' INTO TABLE ITAB.

ITAB now has three rows. The first is empty, the second contains ' Two', and the third ' Three'.

Note

Performance:

The runtime required for the SPLIT statement in the first example of variant 1 is approximately 15 msn (standard microseconds). If you write the sections of f into an internal table, the runtime is around 30 msn.

Related

CONCATENATE, FIND, SEARCH, SHIFT

Additional help

Splitting Strings

Hope this’ll give you idea!!

<b>Pl... award the points.</b>

Good luck

Thanks

Saquib Khan

"Some are wise and some are otherwise"

Read only

Former Member
0 Likes
2,079

Hi Bala,

Consider this code.


report zztest.
data : p_in(8) type c value '20060718',
       p_out(10).


*offset(len)


 p_out+0(4) = p_in+0(4). "First four digits
 p_out+5(2) = p_in+4(2). "Then next 2 digits
 p_out+8(2) = p_in+6(2). "Then next 2 digits

   p_out+4(1) = ''.  "Insert / at 5th position
   p_out+7(1) = ''.  "Insert / at 8th position

write : / p_in,
        / p_out.

Regards,

Arun Sambargi.

Read only

Former Member
0 Likes
2,079

Hi,

Along with offset u can also use length of the variable.

for offset

lv_temp = lv_data+starting_location(Lenght).

starting_location & Lenght ...can be dynamic value.

eg.

lv_data ..........of length 8.

lv_1.............char1 ( lenght 1)

lv_2.............char2 ( lenght 2)

lv_3.............char3 ( lenght 3)

lv_4.............char4 ( lenght 4)

lv_1 = lv_data

first char will be copied

lv_2 = lv_data

1st & 2nd char will be copied

lv_3 = lv_data

1st , 2nd , 3rd char will be copied

lv_4 = lv_data

and so on.