ABAP Keyword Documentation → ABAP Programming Guidelines → Robust ABAP → Assignments, Calculations, and Other Types of Data Access
Avoiding Invalid Values
Other versions: 7.31 | 7.40 | 7.54
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 n
, d
, and t
,
the conversion rules allow any alphanumeric values as the result of an assignment. However, only the following values are valid:
- For type
n
: numbers only
- For type
d
: a calendar date in the format "YYYYMMDD"
- For type
t
: a time in the format "HHMMSS"
For a detailed description of the validity of character-like date and time fields, see also the relevant section of the documentation.
A lossless assignment can be used to force checks on valid values.
Rule
Only assign valid values
In assignments and calculations, data objects are filled with data types n
, d
, and t
with valid values only.
Details
Statements that work with variables with types n
, d
,
or t
can only be guaranteed to behave correctly if values are valid. If
character-like date and time fields are required, it is important to be be aware of their special characteristics.
Since the initial value for variables of type d
is itself not a valid value,
a suitable start value must always be specified by using the addition VALUE
.
Note that in arithmetic calculations with character-like date fields, if an assignment to a target field
with data type d
has 0 as a result value, this is an invalid initial value and may require special treatment.
If the responsibility for filling data objects of the critical data types lies elsewhere,
it is best to always check the validity of their content before use.
Lossless assignments
with the operator EXACT
can be used to do this. Note that the initial value of a character-like date field of type d
is interpreted as valid for a lossless assignment, and the value "00010101" as invalid.
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.
Gutes Beispiel
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 EXACT
operator does not perform a check for compatible types, the data fields are first converted to temporary text strings, and these are checked.
result = EXACT d( CONV string( date2 ) ) -
EXACT d( CONV string( date1 ) ).
CATCH cx_sy_conversion_no_date.
...
ENDTRY.