A grab-bag of frequently needed conversion patterns beyond the core ALPHA/CONV/CORRESPONDING basics already covered in 02-Data-Types.
DATA lv_external TYPE string VALUE '12345'.
DATA lv_internal TYPE string VALUE '000000000000012345'.
" ALPHA = IN : external/display form -> internal form (pads leading zeros)
DATA(lv_padded) = |{ lv_external ALPHA = IN }|.
" ALPHA = OUT : internal form -> external/display form (strips leading zeros)
DATA(lv_stripped) = |{ lv_internal ALPHA = OUT }|.
" NEW <type>( ) creates an anonymous DATA OBJECT and returns a reference to it
DATA(lr_docno) = NEW /scdl/dl_docno_int( CONV #( lv_padded ) ).TYPES: BEGIN OF ty_viqmel,
notif_no TYPE char10,
notif_type TYPE char2,
description TYPE char40,
END OF ty_viqmel.
DATA ls_viqmel TYPE ty_viqmel.
DATA ls_data TYPE ty_viqmel.
ls_viqmel = VALUE ty_viqmel( notif_no = '0000001234'
notif_type = 'M1'
description = 'Initial Notification' ).
ls_data = VALUE ty_viqmel( notif_type = 'M2'
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 ls_data, so it is CLEARED in the result.
ls_viqmel = CORRESPONDING #( BASE ( ls_viqmel ) ls_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 ls_data-notif_no IS NOT INITIAL. ls_viqmel-notif_no = ls_data-notif_no. ENDIF.
DATA lv_timestamp TYPE timestampl.
" Date + time -> timestamp
CONVERT DATE sy-datum TIME sy-uzeit
INTO TIME STAMP lv_timestamp TIME ZONE sy-zonlo.
" Timestamp -> date + time, with inline declaration of the targets
CONVERT TIME STAMP lv_timestamp TIME ZONE sy-zonlo
INTO DATE DATA(lv_datum) TIME DATA(lv_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.
DATA(lv_quantity) = CONV int4( ls_data-value ).
DATA(lv_posnr) = CONV zsm_e_posnr( '000010' ).
DATA(ls_target) = CORRESPONDING zsm_t_data( ls_source )." CONV # works here because the target type comes from the parameter's type
lo_validator->check_appointment( iv_person_id = CONV #( ls_order-person_id ) ).- Prefer
|{ value ALPHA = IN/OUT }|(string template) over the classicalCONVERSION_EXIT_ALPHA_INPUT/OUTPUTfunction module call in new code — same result, less overhead. - 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
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?