I'm reading a csv file with following structure:
Nr;Name;Name;Nr;Name
14;Doe;John;0838;test
15;Doe;John;0835;test2
16;Doe;John;1008;test3
I'm reading the data into a struct type:
types:
begin of ty_dbstr,
Nr TYPE p_pernr, "char 10
Nachname TYPE nachname, "char 25
Vorname TYPE vorname, "char 25
ProdZeit TYPE ZXXX_hours, "dec length 4 decimals 2
Datum TYPE datum, "char 8
end OF ty_dbstr.
With function 'GUI_UPLOAD'
I get a result table lt_raw_data
(TYPE truxs_t_text_data
)
Now I'm processing the data and want to write them in a table of the former declared struct using:
LOOP AT lt_raw_data INTO DATA(ls_csv_line).
CALL METHOD lo_csv_converter->csv_to_structure
EXPORTING
i_data = ls_csv_line
IMPORTING
e_s_data = <fs_wa>.
.
.
.
ENDLOOP.
This works fine for the first 2 entries, but the third produces a dump because it says Overflow when converting 1008
It seems like the 1008 does not fit in a dec of length 4 with 2 decimals, how is this possible?
Dictionary data type:
DEC Length 4 Decimal places 2
means that the total length is 4, i.e. 2 places are used for the whole part of the number and 2 are used for the decimal part.
That means that the maximum value to store in this type is: 99.99
In the documentation it is not so obvious:
DEC for packed numbers This type describes general packed numbers in BCD format. When used, a length and the number of decimal places must be added to the type.
When an ABAP Dictionary type (i.e DEC 4) is used for the type definition in abap, environment performs the mapping to the abap type, in this case from DEC to p, and the length of p will be calcuated based of the length of DEC:
DEC (length 1-31) -> to p (length_of_DEC DIV 2 + 1)
In the above case DEC length 4 decimals 2 is mapped to the [P(3) DEC 2], and the whole part of 3 digits is not enough to store 1008, which leads to overflow. So in this case Dictionary type should be at least DEC length 6 ( 6 DIV 2 + 1 = P(4) ) to store the number.
It also worth mentioning, that an odd number of places should be used in the definition of data types based on the predefined type DEC. Since the values are stored in BCD format, for which in data type p, only an odd number of places is possible, since a half byte is used for the sign. If a data type DEC has an even number of characters, the length of the associated ABAP type p is rounded up and hence contains the next highest odd number of places. This can cause overflows and exceptions.