ABAP Core Part 3: Data Types and Literals
Know ABAP's built-in types — character, numeric, date/time, string and packed — how literals work, where lengths and decimals matter, and which type to choose for business data.
Explanation
📌 KEY POINTS
✅ Character types: c (fixed length), n (numeric text), string (variable length).
✅ Numbers: i/int8 (integers), p (packed decimals for amounts), decfloat16/34, f (avoid for money).
✅ Date d (YYYYMMDD) and time t (HHMMSS) are character-like — but you can calculate with them.
⚠️ Text literal '…' has type c and trims trailing spaces; string literal … keeps them.
━━━━━━━━━━━━━━━━━━━━
▶ WHERE WE ARE
Part 2 declared variables. Now: which type to use and what happens with literals.
━━━━━━━━━━━━━━━━━━━━
▶ 1. CHARACTER-LIKE TYPES
- c LENGTH n — fixed length, padded with blanks. Most dictionary fields (airline, names) are c.
- n LENGTH n — digits only, leading zeros (e.g. document numbers like '0000004711'). Not for calculations.
- string — variable length, ideal for texts built at runtime.
━━━━━━━━━━━━━━━━━━━━
▶ 2. NUMERIC TYPES
- i (4-byte integer), int8 (8-byte) — counts, seats, loop counters.
- p LENGTH x DECIMALS y — packed number: exact decimal arithmetic. Use for amounts and quantities (dictionary CURR/QUAN fields are packed).
- decfloat16 / decfloat34 — decimal floating point, exact decimal rounding with large range.
- f — binary floating point: rounding errors. Avoid for money.
━━━━━━━━━━━━━━━━━━━━
▶ 3. DATE AND TIME
- d — 'YYYYMMDD'; lv_date + 30 gives the date 30 days later; difference of two dates = days.
- t — 'HHMMSS'; arithmetic in seconds.
- Timestamps: utclong or timestamp (p) for time zone independent points in time.
- Initial date is '00000000' — check with IS INITIAL.
━━━━━━━━━━━━━━━━━━━━
▶ 4. BYTE TYPES
x and xstring — binary data (files, hashes, attachments).
━━━━━━━━━━━━━━━━━━━━
▶ 5. LITERALS
- Text field literal 'LH' — type c, trailing blanks ignored.
- String literal
Hello— type string, keeps trailing blanks. - Numeric literal 100 — type i; decimals need a text literal: '12.50' converted to p.
- Use string templates |…| for building texts (Part 4).
━━━━━━━━━━━━━━━━━━━━
▶ 6. CHOOSING THE RIGHT TYPE
- Business fields → dictionary data elements (s_carr_id, s_price) — correct length and semantics.
- Amounts → p (with currency field), quantities → p (with unit).
- Counts → i. Texts → string.
💡 TIP: conversions happen automatically between many types (Part 7), but explicit types avoid surprises.
━━━━━━━━━━━━━━━━━━━━
▶ STEP BY STEP
Run the code example and observe: date arithmetic, packed vs float result, c vs string trailing blanks, and numeric text with leading zeros.
✅ CHECKPOINT: you choose types deliberately, especially for amounts and dates.
Code example
REPORT zabap_core_types. DATA: lv_carr TYPE c LENGTH 3 VALUE 'LH', " fixed length, padded lv_docno TYPE n LENGTH 10 VALUE '4711', " stored as '0000004711' lv_text TYPE string VALUE `Hello `, " string keeps trailing blank lv_len TYPE i, lv_price_p TYPE p LENGTH 8 DECIMALS 2 VALUE '0.10', lv_price_f TYPE f VALUE '0.10', lv_sum_p TYPE p LENGTH 8 DECIMALS 2, lv_sum_f TYPE f, lv_today TYPE d, lv_due TYPE d, lv_days TYPE i, lv_initial TYPE d, lv_c TYPE c LENGTH 4 VALUE 'AB ', lv_str TYPE string VALUE `AB `, lv_airline TYPE s_carr_id, " length and labels from DDIC lv_price TYPE s_price. " packed amount START-OF-SELECTION.* ===== Character-like ===== WRITE: / 'n with leading zeros:', lv_docno. lv_len = strlen( lv_text ). WRITE: / 'string length:', lv_len. " 6 * ===== Numbers: packed vs float for money ===== DO 3 TIMES. lv_sum_p = lv_sum_p + lv_price_p. lv_sum_f = lv_sum_f + lv_price_f. ENDDO. WRITE: / 'packed sum:', lv_sum_p. " exactly 0.30 WRITE: / 'float sum :', lv_sum_f EXPONENT 0 DECIMALS 17. " 0.30000000000000004 * ===== Date and time ===== lv_today = sy-datum. lv_due = lv_today + 30. " 30 days later lv_days = lv_due - lv_today. " 30 WRITE: / 'today:', lv_today, 'due:', lv_due, 'days:', lv_days. IF lv_initial IS INITIAL. " '00000000' WRITE: / 'empty date is initial'. ENDIF. * ===== Text literal vs string literal ===== lv_len = strlen( lv_c ). WRITE: / 'c length (trailing blanks ignored):', lv_len. " 2 lv_len = strlen( lv_str ). WRITE: / 'string length (blanks kept):', lv_len. " 4 * 💡 Modern syntax (7.40+): DATA(lv_due) = lv_today + 30 derives the type inline;* |{ strlen( lv_text ) }| embeds expressions in string templates.Real project scenario
A custom commission calculation used type f for amounts because "it has more precision". Month-end totals differed from FI postings by a few cents on thousands of documents, and auditors asked for an explanation.
The team switched all amount variables to dictionary-based packed types, rounded explicitly where business rules required, and added a unit test comparing totals with FI. The differences disappeared.
Common mistakes
- Using type f for money.
- Calculating with n fields.
- Expecting text literals to keep trailing spaces.
- Generic c fields for business keys instead of dictionary types.
- Not checking dates for IS INITIAL.
Best practices
- Use dictionary types for business data.
- Packed (p) or decfloat for amounts; never f.
- string for dynamic texts; templates for building them.
- Use date arithmetic directly on type d.
- Be explicit about lengths and decimals in local types.
Interview angle
Questions: "Which data type for amounts?", "Difference between c, n and string?", "Text vs string literal?"
Strong answer: amounts use packed p (or decfloat), never f because of binary rounding; c is fixed-length, n numeric text with leading zeros, string variable length; text literals '…' are type c without trailing blanks, string literals … keep them; dates are type d with day arithmetic. Prefer dictionary types for business fields.
Practice this lesson
Quick check
1. Which type should hold an amount such as a ticket price?
2. Which data type stores an ABAP packed number with fixed decimals?
3. Which type preserves leading zeros?
Interview questions
1. What are the elementary data types in ABAP and which one is best for money?
Answer it in your own words first, then compare.
2. Which ABAP data types do you choose for amounts, document numbers, dates and free texts, and why?
Answer it in your own words first, then compare.
Reference-verified: the steps are checked against official SAP documentation and tutorials, and object and field names in the sample code are checked for consistency. The code has not been compiled or run on an SAP system by ERPClimb. Syntax marked release-dependent varies by SAP release, so check it in your own system.
More lessons in this topic
- ABAP Core Part 1: Your First ABAP Program and the Development Tools
- ABAP Core Part 10: Clean ABAP Basics for Maintainable Code
- ABAP Core Part 2: DATA, TYPES, CONSTANTS, Work Areas and Internal Tables
- ABAP Core Part 4: Control Flow and Building Output Texts
- ABAP Core Part 5: Selection Screens — PARAMETERS, SELECT-OPTIONS and Validation
- ABAP Core Part 6: System Fields — sy-subrc, sy-tabix, sy-datum and More
- ABAP Core Part 7: Operators, Calculations and Type Conversion
- ABAP Core Part 8: Messages and User-Friendly Error Handling