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

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 literal Details
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 literal Syntax
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 UTCLONGDATN, 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 literal Details
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 INT1INT2INT4, 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 literal Syntax
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 INT1DECDECFLOAT34, 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:

Sara Sampaio

Sara Sampaio

Author Since: March 10, 2022

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x