SAP UNPACK ABAP Statements

Get Example source ABAP code based on a different SAP table
  


Standard SAP Help for UNPACK

UNPACK

Short Reference
• UNPACK ABAP Statement
• TO UNPACK


ABAP Syntax UNPACK source TO destination.


What does it do? This statement converts the content of the data object source in accordance with special rules and assigns the converted content to the data object destination. source expects the data type p of length 16 without decimal places. Operands of data type decfloat16 or decfloat34 cannot be used. The data type of destination must be character-like and flat.

The conversion is performed according to the following rules: If the data type of source is not of type p with length 16 and without decimal places, then the content of source is converted to this data type. Contrary to the rules described in Conversion Rules for Elementary Data Types, any decimal separator in source is completely ignored.
The digits of the interim result are assigned to data object destination right-aligned and without a sign. Any surplus positions in destination are padded with zeros on the left. If the length of destination is not sufficient, the assigned value is truncated on the left.

Latest notes:The function of the statement UNPACK is based on the fact that the BCD representation of a decimal place corresponds to the second half byte of code of a digit in most character representations. This transformation is generally known as 'Unpacking'. The statement PACK used for packing is obsolete and can be replaced by a regular assignment. If destination is specified as an untyped field symbol or an untyped formal parameter and is not flat and character-like when the statement is executed, this results in an exception (that cannot be handled) in Unicode programs. In non-Unicode programs, an exception only occurs if deep types are used, while flat types are handled like character-like types.



Example ABAP Coding
After the assignments are made, char1 and
char2 contain the values '123,456' and '0000123456'. DATA: pack TYPE p LENGTH 8 DECIMALS 3 VALUE '123.456',
char1 TYPE c LENGTH 10,
char2 TYPE c LENGTH 10.

char1 = pack.
UNPACK pack TO char2.



Runtime Exceptions

Catchable Exceptions
CX_SY_CONVERSION_NO_NUMBER
Reason for error:
Source field cannot be interpreted as a number
Runtime error:
CONVT_OVERFLOW

CX_SY_CONVERSION_OVERFLOW
Reason for error:
Overflow during conversion (type p)
Runtime error:
BCD_OVERFLOW


Non-catchable Exceptions
Reason for error:
Source field (type p) contains an incorrect BCD format
Runtime error:
BCD_BADDATA

Return to menu