Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
AndreaUS
Product and Topic Expert
Product and Topic Expert

Classic ABAP offers untyped literals , which are interpreted as either character literals or as numeric literals. The new kids on the block are typed literals. They are available in almost all data types, but are only supported as operands in  ABAP SQL and ABAP CDS. This blog post provides an overview of literals in ABAP at one glance. Literals are part of the basic ABAP ABC, and ABAP developers should be familiar with them.

Definition

A literal is a direct, character-like specification of a value in the ABAP source code of an ABAP program or in a CDS object. There are two types of literals: typed and untyped literals. Typed literals specify their data type explicitly, while untyped literals only specify a value and the data type is derived implicitly.

Literals in classic ABAP

The classic ABAP programming language offers only untyped literals.

Type of literalDetails
Numeric literal
    • Can be an integer data type except for int8.
    • If the value is not in the value range of an integer type, the literal is interpreted as a packed number of data type p.
    • No decimal separator is allowed.
Character literal
    • Can have either the data type or the data type string.

For all other data types, the ABAP Assignment and Conversion Rules are applied, which may lead to unexpected results.

The literal operator & can be used to concatenate literals. It is available in ABAP and ABAP SQL, but not in ABAP CDS.

Literals in ABAP SQL

Untyped literals in ABAP SQL are the same as ABAP literals, and the same rules apply.
As of ABAP release 7.80 (quarterly release), 7.55 (on-prem), ABAP SQL also supports typed literals:

Type of literalSyntax
Integer literal
    • INT1`...`
    • INT2`...`
    • INT4`...`
    • INT8`...`
Packed number literal
    • DEC`...`
Decimal floating point literal
    • DECFLOAT16`...`
    • D16N`...`
    • DF16_RAW`...`
    • D16R`...`
    • DECFLOAT34`...`
    • D34N`...`
    • DF34_RAW`...`
    • D34R`...`
    • DF16_DEC`...`
    • D16D`...`
    • DF34_DEC`...`
    • D34D`...`
Binary floating point literal
    • FLTP`...`
Text field literal
    • CHAR`...`
Text string literal
    • STRING`...`
    • SSTRING`...`
Byte field literal
    • RAW`...`
Byte string literal
    • RAWSTRING`...`
Numeric text literal
    • NUMC`...`
Client literal
    • CLNT`...`
Language key literal
    • LANG`...`
Date literal
    • DATS`...`
    • DATN`...`
Time literal
    • TIMS`...`
    • TIMN`...`
Time stamp literal
    • UTCLONG`...`
Currency field literal
    • CURR`...`
Currency key literal
    • CUKY`...`
Quantity field literal
    • QUAN`...`
Unit key literal
    • UNIT`...`

Example:
The following example uses typed literals of types UTCLONG, DATN, and INT8 in different operand positions.

 

SELECT SINGLE 
       FROM demo_expressions 
       FIELDS 
         utcl_add_seconds( utclong`2020-04-01T12:01:01,2`,50 ) AS utcl, 
         datn_add_months( datn`17890101`,15 ) AS add_months, 
         int8`9999999999999999` AS int8
INTO @DATA(result).

 

Without typed literals, there would be the following problems:

  • Untyped literals are only available as character literals of type c or string, or as numeric literals of type i or p. The data types UTCLONG and DATN are not available as untyped literals. The ABAP SQL cast expression is also not possible in the example shown, because UTCLONG and DATN are not valid target types.
  • There are no numeric untyped literals of type INT8. Without a typed literal, 9999999999999999 would be interpreted as packed number.

The value of typed literals is checked at compile time. A value that is not within the value range of the specified data type results in a syntax check error.
Example:
The following typed literal specifies a value that is not valid for the data type UTCLONG. A syntax check error occurs at compile time.

 

...
utcl_add_seconds( utclong`2020-13-01T12:01:01,2`,50 ) AS utcl
...

 

The following error message occurs:

In contrast, untyped literals in classic ABAP are checked at runtime, and incorrect values lead to runtime errors. Typed literals are therefore more convenient for developers.

Literals in ABAP CDS

Similar to classic ABAP, classic CDS DDIC-based views offer untyped numeric and untyped character literals:

Type of literalDetails
Character literal
    • If the string consists of digits, it is interpreted as a numeric text literal of data type NUMC.
    • Otherwise, it is interpreted as data type CHAR.
Numeric literal
    • Either an integer literal of data type INT1, INT2, INT4, or INT8.
    • If the literal has a decimal point, it is interpreted as a binary floating point literal of data type FLTP.

As of ABAP release 7.83 (quarterly release), 7.56 (on-prem), typed literals are also available in ABAP CDS. They are supported in CDS view entities and in CDS hierarchies.

Type of literalSyntax
Integer literal
    • abap.int1'...'
    • abap.int2'...'
    • abap.int4'...'
    • abp.int8'...'
Packed number literal
    • abap.dec'...'
Decimal floating point literal
    • abap.decfloat16'...'
    • abap.d16n'...'
    • abap.decfloat34'...'
    • abap.d34n'...'
Binary floating point literal
    • abap.fltp'...'
Text field literal
    • abap.char'...'
Text string literal
    • abap.string'...'
    • abap.sstring'...'
Byte field literal
    • abap.raw'...'
Byte string literal
    • abap.rawstring'...'
    • abap.rstr'...'
Numeric text literal
    • abap.numc'...'
    • abap.clnt'...'
    • abap.lang'...'
Client literal
    • abap.clnt'...'
Language key literal
    • abap.lang'...'
Date literal
    • abap.dats...'
    • abap.datn'...'
Time literal
    • abap.tims'...'
    • abap.timn'...'
Time stamp literal
    • abap.utclong'...'
    • abap.utcl'...'
Currency field literal
    • abap.curr'...'
Currency key literal
    • abap.cuky'...'
Quantity field literal
    • abap.quan'...'
Unit key literal
    • abap.unit'...'

Example
The following example uses typed literals of types INT1, DEC, DECFLOAT34, and D16N in different operand positions.

 

@EndUserText.label: 'CDS view entity, numeric typed literals' 
define view entity DEMO_CDS_NUMERIC_TYPED_LIT 
  as select from demo_ddic_types 
{ 
  key id, 
      abap.int1'1'  as int1, 
      abap.dec'.15' as dec_literal, 
      abap.decfloat34'123.456E789' * 100 as ArithmField 
}
where 
  d16n = abap.d16n'123.45' 

 

Conclusion

Typed literals make life easier for developers and reduce they the potential for errors. Unfortunately, they are not available in the classic ABAP programming language, but only in ABAP SQL and ABAP CDS.

All of this information can also be found in the ABAP Keyword Documentation. This blog post only highlights the topic for those who are interested.

Docu:

You can acquire more skills on the modern ABAP programming available on SAP BTP and SAP S/4HANA in the SAP Learning Journeys, such as

More role-based learning resources and opportunities to get certified are available in one place on the SAP Learning site: SAP Learning

3 Comments