Skip to content

ABAP Keyword Documentation →  ABAP - Dictionary →  ABAP CDS in ABAP Dictionary →  ABAP CDS - Data Definitions →  ABAP CDS - DDL for Data Definitions →  ABAP CDS - DEFINE VIEW →  ABAP CDS - SELECT →  ABAP CDS - SELECT, Operands and Expressions 

ABAP CDS - arith_expr

Other versions: 7.31 | 7.40 | 7.54

Syntax


... [-]operand1 [+|-||/ [-]operand2 [+|-||/ [-]operand3 ... ]] ...

Effect

Arithmetic expression in a SELECT statement of a CDS view. An arithmetic expression uses arithmetic operators to calculate a numeric value from numeric operands. The possible operators are as follows:

Operator Meaning
+ Adds the operands
- Subtracts the right operand from the left
* Multiplies the operands
/ Divides the left operand by the right

A - before an operand multiplies the operand by -1. The data type of the operands must be numeric and be based on one of the built-in data types INT1, INT2, INT4, INT8, DEC, CURR, QUAN, DECFLOAT16, DECFLOAT34, or FLTP. The following can be specified:

The subexpressions of an arithmetic expression can be placed in parentheses (...).

The following table shows which data types can be linked using the operators +, -, and * and the data type of the result:

+, -, * INT1 INT2 INT4 INT8 DEC CURR QUAN DECFLOAT16 DECFLOAT34 FLTP
INT1 INT4 INT4 INT4 INT8 DEC CURR QUAN DECFLOAT16 DECFLOAT34 -
INT2 INT4 INT4 INT4 INT8 DEC CURR QUAN DECFLOAT16 DECFLOAT34 -
INT4 INT4 INT4 INT4 INT8 DEC CURR QUAN DECFLOAT16 DECFLOAT34 -
INT8 INT8 INT8 INT8 INT8 DEC CURR QUAN DECFLOAT16 DECFLOAT34 -
DEC DEC DEC DEC DEC DEC CURR QUAN DECFLOAT16 DECFLOAT34 -
CURR CURR CURR CURR CURR CURR CURR DEC DECFLOAT16 DECFLOAT34 -
QUAN QUAN QUAN QUAN QUAN QUAN DEC QUAN DECFLOAT16 DECFLOAT34 -
DECFLOAT16 DECFLOAT16 DECFLOAT16 DECFLOAT16 DECFLOAT16 DECFLOAT16 DECFLOAT16 DECFLOAT16 DECFLOAT16 DECFLOAT34 -
DECFLOAT34 DECFLOAT34 DECFLOAT34 DECFLOAT34 DECFLOAT34 DECFLOAT34 DECFLOAT34 DECFLOAT34 DECFLOAT34 DECFLOAT34 -
FLTP - - - - - - - - - FLTP

The following table shows which data types can be linked using the operator / and the data type of the result:

/ DECFLOAT16 DECFLOAT34 FLTP
DECFLOAT16 DECFLOAT16 DECFLOAT34 -
DECFLOAT34 DECFLOAT34 DECFLOAT34 -
FLTP - - FLTP

Note the following special conditions:

  • If an expression contains an operand of type DEC, CURR or QUAN, the expression is a decimal expression. In this case, the syntax check checks that the result of each operation is in the value range of the type DEC with length 31 and a maximum of 14 decimal places. If any operands are specified that could produce other values, a syntax error occurs.
  • If an expression has an operand of type DECFLOAT16 or DECFLOAT34, then it is a decimal floating point expression.
  • If an expression has an operand of type FLTP, then it is a binary floating point expression, in which all operands must be of type FLTP.
  • When a division is performed with the operator /, a floating point expression is required. This means the operands must be of type DECFLOAT16, DECFLOAT34, FLTP, or numeric literals with fractional digits and the right operand cannot have the value 0.

Arithmetic expressions can be used as elements of a SELECT list, where they need alternative element names defined using AS.


Notes

  • When a division is performed with two numbers of type DEC, the SQL function DIVISION can be used.

  • To convert operands into the appropriate types, CAST expressions can be used. The built-in conversion function FLTP_TO_DEC can be used for the specific task of converting operands of type FLTP to packed numbers.

Example

SELECT list of a CDS view with arithmetic expressions.

@AbapCatalog.sqlViewName: 'SALES_ORDER_VW'
define view sales_order as
  select from snwd_so
         association [1..*] to snwd_so_i as _item
           on snwd_so.node_key = _item.parent_key
         { key snwd_so.node_key,
               gross_amount,
               gross_amount - tax_amount as pre_tax_amount,
               cast(gross_amount as abap.fltp)
                 + (cast( -gross_amount as abap.fltp) * 0.03)
                   as reduced_amount,
            cast(gross_amount as abap.fltp) * 0.03 as overall_savings,
            _item.so_item_pos as item_position,
            _item.gross_amount as item_gross_amount,
            cast(_item.gross_amount as abap.fltp) * 0.97 as item_savings
}