In this chapter we will learn Elementary data types but before that let us understand Types and Objects.
TYPES and OBJECTS
Types are descriptions and are used to declare variables ( data objects ) in ABAP programs. Types do no occupy memory whereas data objects occupy their own memory space.
ABAP standard Data types
These are known as pre-defined elementary data types. The ABAP Data types can be used to declare data objects, interface parameters, input/output fields and so on.
ABAP Predefined numeric data types are :
Standard type | Description |
I, INT8 | type for integer, fixed length 4 for ( I ), fixed length 8 for ( INT8 ) |
F | type for float number ( F ), fixed length 8 |
P | packed decimals |
DECFLOAT16, DECFLOAT34 | DECFLOAT16 ( 8 bytes with 16 decimal places ), DECFLOAT34 ( 16 bytes with 34 decimal places ) |
ABAP Predefined non-numeric data types are :
Standard type | Description |
C | Type for character string |
N | Type for numerical character string |
D | type for date ( D ), format YYYYMMDD, fixed length 8 |
T | type for time ( T ), format HHHHMMSS, fixed length 6 |
X | Type for byte sequence – Hexadecimal string) |
STRING | dynamic length |
XSTRING | dynamic length hexadecimal string ( byte sequence ) |
SAP ABAP Elementary Data types – Complete or Incomplete
The above mentioned ABAP elementary data types are either Complete ABAP Elementary types – they have fixed length or Incomplete ABAP Elementary types – these do not contain a fixed length and, we need to specify the length of the variable.
ABAP Elementary Data types Complete
Standard type | Syntax |
I, INT8 | DATA lv_int TYPE i. |
F | DATA lv_float TYPE f. |
D | DATA lv_date TYPE d. |
T | DATA lv_time TYPE t. |
DECFLOAT16, DECFLOAT34 | DATA lv_decnumber TYPE decfloat16. |
STRING | DATA lv_string TYPE string. |
XSTRING | DATA lv_xstring TYPE xstring. |
SAP ABAP Elementary Data types – Incomplete
When using incomplete standard data types, developer needs to specify the length of the variable.
Standard Type | Syntax |
C | DATA lv_name TYPE C LENGTH 10. |
N | DATA lv_number TYPE N LENGTH 5. |
X | hexadecimal type ( byte sequence ) |
P | DATA lv_precision TYPE P LENGTH 5 DECIMALS 2. |
Apart from elementary types, we also have complex types and reference types in ABAP.
In ABAP, You can define data types either locally in the declaration part of a program ( using the TYPES keyword ) or globally in the ABAP Dictionary. Types declared by user are know as user-defined types and types given by SAP are known as standard types.
Type Conversion
When moving data between data objects of different types, automatic type conversion is performed by ABAP. This automatic type conversion is followed by conversion rules predefined in ABAP layer.
DATA LV_CHAR TYPE C LENGTH 5 VALUE 12345.
DATA LV_INT TYPE I.
LV_INT = LV_CHAR.
As lv_char holds data which can be stored in a variable of type integer, the above assignment will work.
DATA LV_CHAR TYPE C LENGTH 5 VALUE 'ABC123'.
DATA LV_INT TYPE I.
LV_INT = LV_CHAR.
The above assignment will fail throwing a runtime error CX_SY_CONVERSION_NO_NUMBER as the data of lv_char cannot be kept in a variable of type integer.
DATA LV_CHAR TYPE C LENGTH 5 VALUE 12345.
DATA LV_INT TYPE I.
LV_INT = LV_CHAR + 10.
This system will attempt type conversion before performing the arithmetic operation. Instead of relying on system type conversion, it is advised to use correct TYPE based on the data to be processed. For example, to store date and time information, use TYPE d and t respectively, instead of any other TYPE.
DATA LV_DATE TYPE D VALUE '20220101'.
WRITE LV_DATE.
As it is TYPE D, system setting’s or user settings will be applied to display the output.