Tuesday 27 October 2015

Offset Concept in SAP ABAP

Most of the time in ABAP, we may have to read the part of data of a field (say X) and populate to other field (Say Y). In this case we have to use the offset concept to read the value.

Example:

SY-DATUM is the standard SAP field, which holds the current system date. The format of the date in SY-DATUM field will be YYYYMMDD.

Where,
  • YYYY => Year
  • MM => Month
  • DD => Day

In most of the cases we may have to read the value of month form the SY-SATUM and pass to some variable. Similarly we may have to read the value of year & day.


Below is a simple SAP ABAP program which gives clear picture on how to use offset concept.

======================

REPORT  ZVTEST_PGM_1.

DATAl_day(2)   TYPE c" Day
      l_month(2TYPE c" Month
      l_year(4)  TYPE c" Year

*--Print the value of Current System Date (YYYYMMDD)
WRITE:'Current System Date : ' sy-datum.

*--Use offset concept & populate the Year, Month & Day value to Local valriables

*--Year: Starting form Zeroth Position next 4 characters
l_year sy-datum+0(4).

*--Month: Starting form fourth Position next 2 characters
l_month sy-datum+4(2).

*--Day: Starting form sixth Position next 2 characters
l_day sy-datum+6(2).

*--Print value of Year, Month & date which is stored in local variables
WRITE:'Year  : 'l_year.
WRITE:'Month : 'l_month.
WRITE:'Day   : 'l_day.

======================

Hope post explained the OFFSET concept which we generally use in SAP ABAP.

Will share more useful articles soon.

Thanks & Regards,
Venugopal M N
SAP BI-ABAP Consultant 

Monday 12 October 2015

ABAP Program to Convert Value Stored In a Variable to UPPER/LOWER Case

In general, most of the time in SAP, we may need to convert the value of the String or Character field (say name, city, etc..) to UPPER case of to LOWER case.

We can achieve the functionality using the simple ABAP syntax provided by SAP.

Syntax:
Assuming the P_STR is a variable which is of type STRING or CHAR (C).

To Convert to UPPER case:
To convert value stored in the P_STR to UPPER case, we can use the below syntax.
TRANSLATE p_str TO UPPER CASE.

To Convert to LOWER case:
To convert value stored in the P_STR to LOWER case, we can use the below syntax.
TRANSLATE p_str TO UPPER CASE.

Program:
REPORT  ZVTEST_PGM_1.

*--Parameter for User Input
PARAMETERSp_str TYPE string.

*--Convert to Upper Case
TRANSLATE p_str TO UPPER CASE.
*--Print Value in Output
WRITE:'Upper Case Value : 'p_str.

*--Convert to Lower Case
TRANSLATE p_str TO LOWER CASE.
*--Print Value in Output
WRITE:'Lower Case Value : 'p_str.

================================
Save & Activate.

Enter the input string ("VENUgopal NanjaPPA") & execute (F8).


Below is the converted output.

Thanks & Regards,
Venugopal M Nanjappa