ABAP (Advanced Business Application Programming) is a versatile programming language integral to SAP systems, enabling developers to handle various data types efficiently. Among the most common tasks in SAP development are string handling and date manipulations. Mastering these operations is essential for processing business data accurately and delivering reliable applications.
Strings in ABAP represent sequences of characters and are used extensively for managing textual data such as names, addresses, and descriptions.
There are several ways to declare string variables:
DATA: lv_string TYPE string,
lv_char10 TYPE c LENGTH 10,
lv_text TYPE text255.
string: Variable length string.c: Fixed length character field.text255: Text field up to 255 characters.Combine multiple strings using the CONCATENATE statement or the && operator.
DATA: lv_fullname TYPE string,
lv_firstname TYPE string VALUE 'John',
lv_lastname TYPE string VALUE 'Doe'.
CONCATENATE lv_firstname lv_lastname INTO lv_fullname SEPARATED BY space.
WRITE lv_fullname.
" Using && operator
lv_fullname = lv_firstname && ' ' && lv_lastname.
WRITE lv_fullname.
Extract parts of a string using OFFSET and LENGTH or the + operator.
DATA: lv_text TYPE string VALUE 'SAP ABAP Developer',
lv_substring TYPE string.
lv_substring = lv_text+4(4). " Extract 4 characters starting from position 5
WRITE lv_substring. " Outputs 'ABAP'
Find the length of a string using the STRLEN function.
DATA: lv_length TYPE i.
lv_length = STRLEN( lv_fullname ).
WRITE: / 'Length:', lv_length.
Find the position of a substring using FIND.
DATA: lv_pos TYPE i.
FIND 'ABAP' IN lv_text MATCH OFFSET lv_pos.
IF sy-subrc = 0.
WRITE: / 'Substring found at position:', lv_pos + 1.
ELSE.
WRITE: / 'Substring not found'.
ENDIF.
TO_UPPER / TO_LOWER to convert case.REPLACE to substitute parts of strings.CONDENSE to remove extra spaces.lv_text = to_upper( lv_text ).
WRITE lv_text. " Outputs 'SAP ABAP DEVELOPER'
REPLACE ALL OCCURRENCES OF 'ABAP' IN lv_text WITH '4A'.
WRITE lv_text. " Outputs 'SAP 4A DEVELOPER'
CONDENSE lv_text.
WRITE lv_text. " Removes multiple spaces
Dates are critical in business processes for calculations, reporting, and validations. ABAP provides a range of built-in types and functions to work with dates.
D: Date type in the format YYYYMMDD (e.g., 20230523).T: Time type HHMMSS.DATA: lv_date TYPE d,
lv_today TYPE d VALUE sy-datum.
sy-datum is a system field holding the current date.You can assign dates as literals or system variables.
lv_date = '20250101'.
WRITE lv_date.
Add or subtract days using + or - operators.
DATA: lv_new_date TYPE d.
lv_new_date = lv_today + 10.
WRITE: / 'Date after 10 days:', lv_new_date.
lv_new_date = lv_today - 5.
WRITE: / 'Date 5 days ago:', lv_new_date.
Use CL_ABAP_DATFM class or simple subtraction for day differences.
DATA: lv_diff TYPE i,
lv_start_date TYPE d VALUE '20230101',
lv_end_date TYPE d VALUE '20230523'.
lv_diff = lv_end_date - lv_start_date.
WRITE: / 'Days between:', lv_diff.
Convert string in format YYYYMMDD to date using CONV or |...| templates.
DATA: lv_date_str TYPE string VALUE '20250501',
lv_date_converted TYPE d.
lv_date_converted = CONV d( lv_date_str ).
WRITE lv_date_converted.
Use WRITE statement with formatting options.
WRITE lv_today DATE TIME.
Or use function modules like DATE_TO_EXTENDED for user-friendly formats.
To work with timestamp-like values, use combined date and time fields or classes like CL_ABAP_TSTMP.
DATA: lv_name TYPE string VALUE 'Jane Smith',
lv_join_date TYPE d VALUE '20230101',
lv_today TYPE d VALUE sy-datum,
lv_tenure TYPE i.
" Calculate tenure in days
lv_tenure = lv_today - lv_join_date.
WRITE: / 'Employee:', lv_name,
/ 'Join Date:', lv_join_date,
/ 'Tenure (days):', lv_tenure.
Effective handling of strings and dates is fundamental in ABAP programming for SAP applications. ABAP offers a rich set of commands and functions to manipulate textual data and perform date calculations accurately. By mastering these basics, developers can build reliable and user-friendly applications tailored to complex business needs.