SAP AVOIDING INVALID VALUES - Guide
Get Example source ABAP code based on a different SAP table
GUIDELINE 6.13
Avoiding Invalid Values
ABAP_BACKGROUND
For performance reasons, the ABAP runtime environment does not check whether the target field contains a valid value after each and every assignment. Particularly for target fields of character-like data types
For a detailed description of the validity of character-like date 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,
Bad example
The following source code shows a case where the conversion rules in ABAP can lead to problems if not used properly in combination with character-like date fields. The literals can be passed to the character-like 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, the expected result would be 9.
date2 TYPE d,
result TYPE i.
date1 = '07092009'.
date2 = '16092009'.
result = date2 - date1.
#Good example
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 using the appropriate typed literals.
date2 TYPE d,
result TYPE i.
date1 = d`20090907`.
date2 = d`20090916`.
result = date2 - date1.
The following source code shows how to check whether the character-like 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.