Lifecycle:
CURRENT / RECOMMENDED. Older forms you will still meet are labelled where they appear. See 21-Classic-vs-Modern-ABAP.
A grab-bag of frequently needed conversion patterns beyond the core ALPHA/CONV/CORRESPONDING basics already covered in 02-Data-Types.
📝 Contextual snippet —
zsm_e_document_idis a placeholder character-like data element.
DATA external_value TYPE string VALUE '12345'.
DATA internal_value TYPE string VALUE '000000000000012345'.
DATA document_id TYPE c LENGTH 10 VALUE '12345'.
" ALPHA = IN : external/display form -> internal form (pads leading zeros).
" A string has no fixed length, so give one with WIDTH
DATA(padded_value) = |{ external_value ALPHA = IN WIDTH = 18 }|. " 000000000000012345
" A fixed-length field is padded to its own length
DATA(padded_id) = |{ document_id ALPHA = IN }|. " 0000012345
" ALPHA = OUT : internal form -> external/display form (strips leading zeros)
DATA(stripped_value) = |{ internal_value ALPHA = OUT }|.
" NEW <type>( ) creates an anonymous DATA OBJECT and returns a reference to it
DATA(document_id_ref) = NEW zsm_e_document_id( padded_id ).
⚠️ ALPHA = INon astringadds no zeros by itself. According to the ABAP Keyword Documentation, the result length is theWIDTHvalue, the length of a fixed-length target the template is assigned to, or otherwise the length of the operand. For the five-character string'12345'that is five characters, so nothing is padded.
TYPES: BEGIN OF notification,
notif_no TYPE char10,
notif_type TYPE char2,
description TYPE char40,
END OF notification.
DATA notification_data TYPE notification.
DATA change_data TYPE notification.
notification_data = VALUE notification( notif_no = '0000001234'
notif_type = 'T1'
description = 'Initial Notification' ).
change_data = VALUE notification( notif_type = 'T2'
description = 'Updated Notification' ).
" BASE supplies the starting values; the source then overwrites EVERY
" identically-named component - including with initial values.
" Here notif_no is initial in change_data, so it is CLEARED in the result.
notification_data = CORRESPONDING #( BASE ( notification_data ) change_data ).
⚠️ CORRESPONDING #( BASE ( a ) b )is not a "merge non-initial fields" operator. It starts fromaand then assigns all matching components fromb, initial ones included.BASEis useful when the source structure has fewer components than the target — the extra target components keep their values. If you genuinely want "only overwrite where the source has a value", write that condition explicitly:IF change_data-notif_no IS NOT INITIAL. notification_data-notif_no = change_data-notif_no. ENDIF.
DATA time_stamp TYPE timestampl.
" Date + time -> timestamp. A TIMESTAMPL target gets zero fractions of a second;
" an inline declaration here would be of type TIMESTAMP
CONVERT DATE sy-datum TIME sy-uzeit
INTO TIME STAMP time_stamp TIME ZONE sy-zonlo.
" Timestamp -> date + time, with inline declaration of the targets
CONVERT TIME STAMP time_stamp TIME ZONE sy-zonlo
INTO DATE DATA(date) TIME DATA(time).💡 This is the standard conversion used when a UI/Gateway layer sends a
timestamplvalue (for example20240524131025.8750000) that has to become a plain date on the ABAP side.
📝 Contextual snippet —
change_datawith a componentvalue,source, the placeholder data elementzsm_e_posnrand the placeholder structurezsm_s_targetare assumed.
DATA(quantity) = CONV int4( change_data-value ).
DATA(item_number) = CONV zsm_e_posnr( '000010' ).
DATA(target) = CORRESPONDING zsm_s_target( source ).📝 Contextual snippet —
validatorandorderare assumed;check_appointmenthas a completely typed parameterperson_id.
" CONV # works here because the target type comes from the parameter's type
validator->check_appointment( person_id = CONV #( order-person_id ) ).- Prefer
|{ value ALPHA = IN/OUT }|(string template) over the classicalCONVERSION_EXIT_ALPHA_INPUT/OUTPUTfunction module call in new code — give the length withWIDTHor a fixed-length operand, since a string is not padded by itself. - Use
CORRESPONDINGto map structures instead of long field-by-field assignments — but read theBASEnote above before assuming it merges. - Always specify
TIME ZONEexplicitly inCONVERT ... TIME STAMP— omitting it silently uses the wrong zone in multi-time-zone landscapes. - Use
CONV #( )where the target type is derivable from the context (a parameter, a typed assignment) and an explicitCONV <type>( )where it is not.
- Applying
ALPHA = INtwice (double padding), or mixingIN/OUTdirections inconsistently across a codebase. - Expecting
ALPHA = INto pad astringwithoutWIDTH. - Expecting
CORRESPONDING #( BASE ( a ) b )to skip initial source components. It does not. - Forgetting
TIME ZONEin timestamp conversions, causing off-by-hours bugs. - Using
CONV #( '' )on a data element without checking whether an initial value is valid for that domain.
- Explain what the
ALPHAconversion exit does and why SAP key fields (material number, document number) use it. - Explain exactly what
CORRESPONDING #( BASE ( ... ) ... )does to components that are initial in the source. - Be ready to explain the difference between
CONV,CORRESPONDING, and a conversion-exit function module — when is each the right tool?
- 02-Data-Types —
CONV,CORRESPONDINGand type conversions - 09-Modularization — conversion exits
- Date-Time.md — date and time formatting