Saturday, May 10, 2008

SAP ABAP Function Module to Reverse a String

SAP ABAP Function Module to Reverse a String.

You can easily reverse the string using the following function module.

STRING_REVERSE

Please refer the program shown below for details.

REPORT ZEX_STRINGREV .

Parameters: p_string(200).
Data: r_string(100).

CALL FUNCTION 'STRING_REVERSE'
  EXPORTING
    STRING          = p_string
    LANG            = 'E'
 IMPORTING
   RSTRING          = r_string
 EXCEPTIONS
   TOO_SMALL        = 1
   OTHERS           = 2
          .
IF SY-SUBRC <> 0.
 MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

*Inputted String.

Write:/ 'Inputted String', p_string.
SKIP.

*The reverse of the above mentioned string is as follows.
Write:/ r_string.


If the input is as follows

'ABCDEFGH'

Then the output is

Reverse a String                         
                                         
Inputted String                          
ABCDEFGH                                 
                                         
Reverse of the above string is           
HGFEDCBA

                                
                                         


SAP ABAP adding days to Dates

SAP ABAP Using the ABAP inbuilt functionality to add Sates.

The easiest way to add or subtract days from any date filed in ABAP is shown below. Basically ABAP has an inbuilt functionality with the help of which you can add or subtract dates from the date field.

See the example given below.

REPORT ZEX_DATEADD .

*Using the ABAP inbuilt functionality to add dates.
* Data Declaration
Parameters: P_DATE1       TYPE D.

Data:       D_DATE1       TYPE D.

* 15 Days after the input date
D_Date1 = P_DATE1 + 15.
Write:/ 'Input Date', P_DATE1, '15 Days after the input date.'.
WRITE / D_DATE1.
SKIP.
* 50 Days after the input date
D_Date1 = P_DATE1 + 50.
Write:/ 'Input Date', P_DATE1, '50 Days after the input date.'.
WRITE / D_DATE1.
SKIP.
* 500 Days after the input date
D_Date1 = P_DATE1 + 500.
Write:/ 'Input Date', P_DATE1, '500 Days after the input date.'.
WRITE / D_DATE1.
SKIP.

Write:/ 'Going Back in Time'.
SKIP.

* 15 Days before the input date
D_Date1 = P_DATE1 - 15.
Write:/ 'Input Date', P_DATE1, '15 Days before the input date.'.
WRITE / D_DATE1.
SKIP.
* 50 Days before the input date
D_Date1 = P_DATE1 - 50.
Write:/ 'Input Date', P_DATE1, '50 Days before the input date.'.
WRITE / D_DATE1.
SKIP.
* 500 Days before the input date
D_Date1 = P_DATE1 - 500.
Write:/ 'Input Date', P_DATE1, '500 Days before the input date.'.


If you input the following date

01012000

Then the output would be as follows.

Adding Date                                                 
                                                            
Input Date 01012000 15 Days after the input date.           
01162000                                                    
                                                            
Input Date 01012000 50 Days after the input date.           
02202000                                                    
                                                            
Input Date 01012000 500 Days after the input date.          
05152001                                                    
                                                            
Going Back in Time                                          
                                                            
Input Date 01012000 15 Days before the input date.          
12171999                                                    
                                                            
Input Date 01012000 50 Days before the input date.          
11121999                                                    
                                                            
Input Date 01012000 500 Days before the input date.         
08191998                                                    
                           
                                 
                                                            

SAP ABAP Function Module String Concatenate

SAP ABAP Function Module String Concatenate

In SAP ABAP if you wish to concatenate 2 or 3 strings together then you can use the following function module.

STRING_CONCATENATE_3

The above mentioned function module excepts 3 strings and concatenates them into 1. Please see the example given below.


REPORT ZEX_STRINGCON .

parameters: p_str1(100),
                      p_str2(100),
                      p_str3(100).

Data:       d_str(500).

CALL FUNCTION 'STRING_CONCATENATE_3'
  EXPORTING
    STRING1         = p_str1
    STRING2         = p_str2
    STRING3         = p_str3
 IMPORTING
   STRING           = d_str
 EXCEPTIONS
   TOO_SMALL       = 1
   OTHERS          = 2
          .
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.


Write: d_str.


If the input is as follows.

p_str1 = 'This'
p_str2 = 'Is'
p_str3 = 'ABAP'

The output would be as follows.

String Concatenate     
                      
THISISABAP