12

Arithmetic Operations with different data types in CDS views

 1 year ago
source link: https://blogs.sap.com/2023/03/29/arithmetic-operations-with-different-data-types-in-cds-views/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
March 29, 2023 2 minute read

Arithmetic Operations with different data types in CDS views

Introduction

For the calculation of any two numbers, we need at least one Arithmetic Operator. It basically comprises operations such as Addition [+], Subtraction [-], Multiplication [*], and Division [/]. And there are different data types such as INT, CHAR, DATS…

Content

Usually, we can’t perform a calculation with parameters whose datatypes are different. In this blog, we are going to see how to perform Arithmetic Operations with different data types in CDS views. In the below images, a list of datatypes and their conversion logic is given.

types-of-data.png
all-flow-1.png

     If a built-in data type from ABAP Dictionary is specified for datatype, no further restrictions apply to combinations with “x”. The following rules apply to the other combinations:

  • In combinations using “y”, the target data type must be long enough.
  • In combinations using “z”, the lengths of the data types must match exactly.
  • In the case of combinations with “p” or “d”, no built-in data type from ABAP Dictionary can be specified. A data element must be specified as the target data type instead.
    1. In combinations with “d”, the data element can have a suitable target type in accordance with the table above and with any length.
    2. In combinations with “p”, the data element must have the built-in data type and the same length as the data type of the operand.
{

//Division

  division(10,3,4)                 as value1, // [Ans:3.3333]

  div(55,5)                        as value2, // [Ans:11]

  (10.5  / 3.2)                    as value3, // [Ans:3.28125]

// multiplication

  (10  * 3)                        as value4, // [Ans:30]

  (10 *6 * 3)                      as value5, // [Ans:180]

//Modulo

  mod(2,3)                         as value6, // [Ans:2]

// Addition

  (2 +3)                           as value7, // [Ans:5]

  (2.2 + 3.7)                      as value8, // [Ans:5.9]

// (2 + 3.7) as value8--> is not possible as both are different data types

//subtraction

  (2 -3)                           as value9, // [Ans:-1]

  (21 -1)                          as value10, // [Ans:20]

  (21.5 - 1.7)                     as value11, // [Ans:1.98]

//(21.5 - 1) as value11--> is not possible as both are different data types

//BODMAS

  (2+3-4*3)                        as value12, // [Ans:-7]

  cast( 2+3-4*3 as abap.fltp)/ 2.0 as value13  // [Ans:-3.5]

// As ‘/’ only support fltp data type, we changed to fltp

}

Suppose we have a parameter1 as CHAR datatype but need to perform an Arithmetic operation like addition, we can’t directly change CHAR to INT as shown in the image.

cant-flow.png
flow-1.png
flow-2.png

So, in such a case, we can change CHAR datatype to INT by having NUMC as intermediate,

CHAR –> NUMC –> INT

cast ( cast (parameter1 as abap.numc( 8 )) as  abap.int8)as datatype_int

Conclusion

Using this blog post, it will be easy to understand the Arithmetic Operations with different data types and conversion logic between different data types, especially for beginners. Kindly share your feedback or doubts in a comment section and please follow my Dhanasekar C for future posts. And feel free to ask any questions related to data and analytics in our SAP Community forum and follow up for more related blogs.

Thank you

Reference

ABAP CDS – cast_expr

Related topics

Casting

String Functions


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK