SAP CDS ARITHMETIC EXPRESSION V2



Get Example source ABAP code based on a different SAP table
  


• + ABAP_CDS_OPERATOR_V2
• - ABAP_CDS_OPERATOR_V2
• * ABAP_CDS_OPERATOR_V2
• / ABAP_CDS_OPERATOR_V2

ABAP_CDS_DDL - CDS View Entity, arith_expr

ABAP_SYNTAX
... $[-$]operand1 $[+$|-$|*$|/ $[-$]operand2 $[+$|-$|*$|/ $[-$]operand3 ... $]$] ...

What does it do?
Arithmetic expression in a SELECT statement of a CDS view entity. An arithmetic expression uses arithmetic operators to calculate a numeric value from numeric operands. The possible operators are as follows: OperatorMeaning
+Adds the operands
-Subtracts the right operand from the left
*Multiplies the operands
/Divides the left operand by the right
A minus sign (-) 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, DECFLOAT16 , DECFLOAT34, FLTP, or QUAN. The following can be specified:
Numeric literals
Numeric fields of a data source data_source of the current CDS view entity
Parameter with numeric data type
Path expressions that identify a numeric field of a data source data_source
Built-in functions that return a numeric type
SQL-based scalar functions that return a numeric type
Casting expressions that return a numeric type
Case distinctions that return a numeric type
Aggregate expressions that return a numeric type
Another arithmetic expression
A reuse expression using $projection
The subexpressions of an arithmetic expression can be placed in parentheses (...).
The following table shows which data types can be combined using the operators +, -, and * and the data type of the result: +, -, *INT1INT2 INT4INT8DEC DECFLOAT16 DECFLOAT34FLTPQUAN
INT1INT4INT4INT4INT8 DECDECFLOAT16DECFLOAT34 -QUAN
INT2INT4INT4INT4INT8 DECDECFLOAT16DECFLOAT34 -QUAN
INT4INT4INT4INT4INT8 DECDECFLOAT16DECFLOAT34 -QUAN
INT8INT8INT8INT8INT8 DECDECFLOAT16DECFLOAT34 -QUAN
DECDECDECDECDEC DECDECFLOAT16DECFLOAT34 -QUAN
DECFLOAT16DECFLOAT16DECFLOAT16 DECFLOAT16DECFLOAT16DECFLOAT16 DECFLOAT16DECFLOAT16DECFLOAT34- DECFLOAT16
DECFLOAT34 DECFLOAT34DECFLOAT34DECFLOAT34DECFLOAT34 DECFLOAT34DECFLOAT34DECFLOAT34- DECFLOAT34
FLTP-------FLTP-
QUAN QUANQUANQUANQUAN QUANDECFLOAT16DECFLOAT34-*: DECFLOAT34 lbr +, -: QUAN
The following table shows which data types can be combined using the operator / and the data type of the result: /INT1INT2INT4 INT8 DEC DECFLOAT16DECFLOAT34 FLTPQUAN
INT1DECFLOAT34DECFLOAT34 DECFLOAT34DECFLOAT34DECFLOAT34 DECFLOAT34 DECFLOAT34-DECFLOAT34
INT2DECFLOAT34DECFLOAT34 DECFLOAT34DECFLOAT34DECFLOAT34 DECFLOAT34 DECFLOAT34-DECFLOAT34
INT4DECFLOAT34DECFLOAT34 DECFLOAT34DECFLOAT34DECFLOAT34 DECFLOAT34 DECFLOAT34-DECFLOAT34
INT8DECFLOAT34DECFLOAT34 DECFLOAT34DECFLOAT34DECFLOAT34 DECFLOAT34 DECFLOAT34-DECFLOAT34
DECDECFLOAT34DECFLOAT34 DECFLOAT34 DECFLOAT34DECFLOAT34 DECFLOAT34 DECFLOAT34-DECFLOAT34
DECFLOAT16DECFLOAT34DECFLOAT34 DECFLOAT34DECFLOAT34DECFLOAT34 DECFLOAT16 DECFLOAT34-DECFLOAT34
DECFLOAT34DECFLOAT34 DECFLOAT34 DECFLOAT34DECFLOAT34DECFLOAT34 DECFLOAT34 DECFLOAT34 -DECFLOAT34
FLTP-------FLTP-
QUANDECFLOAT34DECFLOAT34 DECFLOAT34DECFLOAT34DECFLOAT34 DECFLOAT34 DECFLOAT34 -DECFLOAT34
It is also possible to use CDS amount fields and CDS quantity fields in arithmetic expressions. The result type can be a calculated quantity. The rules for amounts and quantities in arithmetic expressions in CDS view entities are explained in topic CDS View Entity, arith_exp, Amounts and Quantities.
Note the following special conditions:
If an expression contains an operand of type DEC, the expression is a decimal expression. In this case, the syntax check checks that the result of each operation is in the value ra nge 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 /, the right operand cannot have the value 0.
If an operand of an arithmetic expression has the null value, the result of the entire arithmetic expression is the null value.
Data type CURR is also possible, but only under the following conditions:
All operands must be of type CURR and have exactly two decimal places.
CURR is only allowed in additions and subtractions (operators + and -). Not in multiplications and divisions.
Operand positions:
Arithmetic expressions can be used as elements of a SELECT list, and there they need alternative element names defined using AS.
An arithmetic expression can be used as the operand of an aggregate expression, a built-in function, a cast expression, or a case distinction.
An arithmetic expression can be used in the condition cds_cond as operand lhs of a complex case distinction.



Latest 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.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
SELECT list of a CDS view entity with arithmetic expressions.
DDLS DEMO_CDS_SO_ARITH_EXPR
ABAP_EXAMPLE_END