SAP AVOIDING INVALID VALUES - Guide
Get Example source ABAP code based on a different SAP table
Avoiding Invalid Values
ABAP_BACKGROUND
For performance reasons, the ABAP runtime framework does not check whether the target field contains a valid value after each and every assignment. Particularly for target fields of the character-like data type
For a detailed description of the validity of date fields and time fields, see also the
A
ABAP_RULE
In assignments and calculations, data objects are filled with data types
ABAP_DETAILS
Statements that work with variables with types
If the responsibility for filling data objects of the critical data types lies elsewhere,
ABAP_EXAMPLE_BAD
The following source code shows a case where the conversion rules in ABAP can lead to problems if not used properly in combination with date fields. The literals can be transferred to the date fields, without raising an exception, to give the values 07092009 and 16092009. Unfortunately, these are interpreted as 09.20.0709 and 09.20.1609, which are invalid dates. During the calculation, they are both converted to the value 0 and the result is 0. Looking at the dates, you would expect the result to be 9.
date2 TYPE d,
result TYPE i.
date1 = '07092009'.
date2 = '16092009'.
result = date2 - date1.
ABAP_EXAMPLE_END
ABAP_EXAMPLE_GOOD
The following source code shows a date calculation that does give the expected result of 9, thanks to valid values in the date fields. The validity of the literal values is guaranteed by the use of the operator
DATA(result) = EXACT d( '20090916' ) - EXACT d( '20090907' ).
CATCH cx_sy_conversion_no_date.
...
ENDTRY.
The following source code shows how you can check whether the date fields are valid in the calculation, if they are not filled in the same program. Since the
result = EXACT d( CONV string( date2 ) ) -
EXACT d( CONV string( date1 ) ).
CATCH cx_sy_conversion_no_date.
...
ENDTRY.
ABAP_EXAMPLE_END