SAP USE CONVERSION RULES - Guide
Get Example source ABAP code based on a different SAP table
Using Conversion Rules
ABAP_BACKGROUND
ABAP contains numerous conversion rules for assignments between data objects of different data types. These rules relate to assignments between:
Assignments between data objects of nearly every different data type are possible. The only prohibited assignments are for data types for data and time specifications. Almost all allowed assignments have a corresponding conversion rule. It is especially important to have rules for assignments between data objects of the same data type, if different technical properties (such as length or number of decimal places) are allowed.
ABAP_RULE
Only assign data objects to each other if the content corresponds to the data type of the target field and produces an expected result. Do not exploit every ABAP conversion rule to its full extent. Consider using
ABAP_DETAILS
The ABAP conversion rules are based on the philosophy that assignments should be allowed between as many combinations of values as possible, without raising exceptions. In this situation, ABAP behaves quite differently from other programming languages. In other languages, assignments between different data types are usually handled much more strictly and special conversion routines or explicit casting for specific requested conversions are used.
Although it is convenient to be able to readily assign all possible data objects to each other, there are also disadvantages, such as the
Even if no invalid values are created, problems still can occur. If valid target values are created from invalid source values, this does not necessarily meet the expectations of the reader and it can make program maintenance considerably difficult. One example of this is the handling of invalid content in the source field in assignments from a character-like type to a byte-like type. Instead of exiting the assignment with an exception, hexadecimal zeros are passed from the first invalid character.
The only solution here are
ABAP_EXAMPLE_BAD
Anyone who is familiar with all the details of the ABAP conversion rules would probably expect an exception when the text in the following source code is assigned to the numeric text. However, only the digits of the text are respected. Therefore, the target field is given the value
num_text TYPE n LENGTH 8.
...
text = '4 Apples + 2 Oranges'.
...
num_text = text.>
ABAP_EXAMPLE_END
ABAP_EXAMPLE_GOOD
This issue is corrected in the source code below. The
text = '4 Apples + 2 Oranges'.
...
TRY.
num_text = EXACT #( text ).
CATCH cx_sy_conversion_error.
...
ENDTRY.>
ABAP_EXAMPLE_END