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

Concatenate internal values into a variable

Former Member
0 Likes
7,042

I have created a internal table of one field to contain a list of text message.

I want to concatenate all the entries in the internal table into single variable separated by ','.

How can I do this?

13 REPLIES 13
Read only

Former Member
0 Likes
3,633

Concatenating Character Strings

The CONCATENATE statement combines two or more separate strings into one.

CONCATENATE c1 ... cn INTO c [SEPARATED BY s].

This statement concatenates the character fields c1 to cn and assigns the result to c. The system ignores spaces at the end of the individual source strings.

The addition SEPARATED BY s allows you to specify a character field s which is placed in its defined length between the individual fields.

If the result fits into c, sy-subrc is set to 0. However, if the result has to be truncated, sy-subrc is set to 4.

DATA: c1(10) TYPE c VALUE 'Sum',

c2(3) TYPE c VALUE 'mer',

c3(5) TYPE c VALUE 'holi ',

c4(10) TYPE c VALUE 'day',

c1 (30) TYPE c,

sep(3) TYPE c VALUE ' - '.

CONCATENATE c1 c2 c3 c4 INTO c5.

WRITE c5.

CONCATENATE c1 c2 c3 c4 INTO c5 SEPARATED BY sep.

WRITE / c5.

The output looks like this:

Summerholiday

Sum - mer - holi - day

In c1 to c5, the trailing blanks are ignored. The separator sep retains them.

Read only

Former Member
0 Likes
3,633

Some function modules for string manipulation have become obsolete and should be replaced by ABAP/4 statements or functions:

STRING_CONCATENATE...->CONCATENATE

STRING_SPLIT...->SPLIT

STRING_LENGTH...->strlen()

STRING_CENTER...->WRITE...TO...CENTERED

STRING_MOVE_RIGHT...->WRITE...TO...RIGHT-JUSTIFIED

Read only

Former Member
0 Likes
3,633

Hi,

Loop at itab into wa.

Cnactenate wa INTO variable SEPERATED by '.'.

ENDLOOP.

Read only

Former Member
0 Likes
3,633

Hi,

DATA:final_string type string,

char(1) type c value ','.

Loop at itab into wa.

concatenate final_string wa-message into final_string separated by char.

Endloop.

now the final value is final_string.

Read only

Subhankar
Active Contributor
0 Likes
3,633

Hi ..

Please check the code

DATA: i_data TYPE STANDARD TABLE OF marc INITIAL SIZE 0,

l_str TYPE string,

wa_data TYPE marc.

SELECT * FROM marc

UP TO 5 ROWS

INTO TABLE i_data.

LOOP AT i_data INTO wa_data.

CONCATENATE wa_data-matnr wa_data-werks INTO l_str SEPARATED BY ','.

WRITE:/ l_str.

ENDLOOP.

Read only

Former Member
0 Likes
3,633

Hi,

Declare a variable string in which u want to save ur concatenated string.

string = ' '.

Loop at internal table.

Concatenate string itab-field into string seperated by ','.

endloop.

Regards,

Surinder

Edited by: SURINDER SINGH OBEROI on Sep 26, 2008 3:25 PM

Read only

Former Member
3,633

Hi,

Check this sample code


REPORT z_sdn.


DATA:
  BEGIN OF fs_tab,
    txt(4),
  END OF fs_tab.


DATA:
  t_tab LIKE
  TABLE OF
        fs_tab.

DATA:
  w_txt TYPE string.


fs_tab-txt = 'ABC'.
APPEND fs_tab-txt TO t_tab.

fs_tab-txt = 'DEF'.
APPEND fs_tab-txt TO t_tab.

fs_tab-txt = 'GHI'.
APPEND fs_tab-txt TO t_tab.

LOOP AT t_tab INTO fs_tab.
  CONCATENATE fs_tab-txt w_txt INTO w_txt SEPARATED BY ','.
ENDLOOP.


WRITE: / w_txt.

Regards

Abhijeet

Read only

Former Member
0 Likes
3,633

hi,

just go through this program.

REPORT ZSHAN_MESG.

Data: begin of istring,

mesg type string,

end of istring.

data: it_string like table of istring with header line.

data: totstring type string.

*appending SOME messages to IT_STRING TABLE

move 'ALV' to it_string-mesg.

append it_string.

move 'BDC method' to it_string-mesg.

append it_string.

move 'LSMW method' to it_string-mesg.

append it_string.

move 'BSP applicaitons' to it_string-mesg.

append it_string.

move 'string operations' to it_string-mesg.

append it_string.

loop at it_string.

write: / it_string-mesg.

endloop.

skip 2.

uline at 1(35).

write:/ 'Total itab CONCATENATED string', /.

uline at 1(35).

skip 1.

*CONCATENATING ALL MESSAGES TO ONE STRING

loop at it_string.

Concatenate totstring it_string-mesg into totstring separated by SPACE.

endloop.

write:/ totstring.

Regards,

Shankar.

Read only

0 Likes
3,633

Thanks buddy.. Did you help me in my problem

Read only

Former Member
0 Likes
3,633

Hi Vimala,

try it this way:

loop at itab into wa.

  concatenate t_string wa-field into t_string separated by ','.

endloop.


write t_string.

With luck,

Pritam.

Read only

Former Member
0 Likes
3,633

Hi,

Here is the sample code for your solution.

REPORT ZAM_CONCATENATE.

tables: spfli.

types: begin of itabtype1,

carrid type sflight-carrid,

end of itabtype1.

data: itab1 type standard table of itabtype1,

wa1 like line of itab1.

data: output1(500) type c,

sep(3) value ' , '.

select carrid from spfli into table itab1.

loop at itab1 into wa1.

if sy-tabix EQ 1.

concatenate output1 wa1-carrid into output1.

else.

concatenate output1 wa1-carrid into output1 separated by SEP.

endif.

endloop.

write : / output1.

thanks

nikita mishra

Read only

Former Member
0 Likes
3,633

Thank you for you help.

I solved this problem using the logic below.

data : str(80).

str = ' '.

loop at itab.

concatenate str itab-stext into str separated by ','.

endloop.

move str+1(79) to text.

Read only

santiagotenti
Explorer
0 Likes
3,633

You can allways do this:

DATA:it_line LIKE tline OCCURS 1 WITH HEADER LINE.

LOOP AT it_line.

      if sy-tabix = 1.

           gw_final-xtline = it_line-tdline.

      else.

            CONCATENATE gw_final-xtline it_line-tdline into gw_final-xtline.

      endif.

ENDLOOP.

Simple