SAP CDS ARITHMETIC EXPRESSION V1



Get Example source ABAP code based on a different SAP table
  


• + ABAP_CDS_OPERATOR
• - ABAP_CDS_OPERATOR
• * ABAP_CDS_OPERATOR
• / ABAP_CDS_OPERATOR

ABAP_CDS_DDL - DDIC-Based View, arith_expr

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

What does it do?
Arithmetic expression in a SELECT statement of a ABAP_CDS_V1_VIEW . 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, CURR, QUAN, DECFLOAT16, DECFLOAT34, or FLTP. The following can be specified:
Numeric literals without a domain prefix
Numeric fields of a data source data_source of the current CDS view.
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
Casting expressions that return a numeric type
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 CURRQUANDECFLOAT16 DECFLOAT34FLTP
INT1INT4INT4INT4INT8 DECCURRQUANDECFLOAT16DECFLOAT34 -
INT2INT4INT4INT4INT8 DECCURRQUANDECFLOAT16DECFLOAT34 -
INT4INT4INT4INT4INT8 DECCURRQUANDECFLOAT16DECFLOAT34 -
INT8INT8INT8INT8INT8 DECCURRQUANDECFLOAT16DECFLOAT34 -
DECDECDECDECDEC DECCURRQUANDECFLOAT16DECFLOAT34 -
CURRCURRCURRCURRCURR CURRCURRDECDECFLOAT16DECFLOAT34 -
QUANQUANQUANQUANQUAN QUANDECQUANDECFLOAT16DECFLOAT34 -
DECFLOAT16DECFLOAT16DECFLOAT16 DECFLOAT16DECFLOAT16DECFLOAT16DECFLOAT16 DECFLOAT16DECFLOAT16DECFLOAT34-
DECFLOAT34DECFLOAT34DECFLOAT34 DECFLOAT34DECFLOAT34DECFLOAT34DECFLOAT34 DECFLOAT34DECFLOAT34DECFLOAT34-
FLTP---------FLTP
The following table shows which data types can be combined using the operator / and the data type of the result: /DECFLOAT16DECFLOAT34 FLTP
DECFLOAT16DECFLOAT16DECFLOAT34-
DECFLOAT34DECFLOAT34DECFLOAT34-
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 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 /, it must be a floating point expression. This means the operands must be of type DECFLOAT16, DECFLOAT34, or FLTP in combinations shown in the table above, or numeric literals with decimal places. Other numeric data types are not possible. 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.



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 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
}
ABAP_EXAMPLE_END