Arithmetic Operations with different data types in CDS views
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.
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.
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.
-
- In combinations with “d”, the data element can have a suitable target type in accordance with the table above and with any length.
- 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.
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
Related topics
Recommend
-
6
vRealize Operations 8.2 Views: filter enhancements Robert Mesropyan posted December 6, 2020...
-
4
Views: Business Hours and not only in vRealize Operations 8.4 I am delighted to share that the exciting features promised in
-
3
Two Different Views of What Decoupled Code Means 2021-05-18 - (5 min read) I had a...
-
15
Part 6-7 Diving deeper into Views and WidgetsIn my previous blog post, I introduced parts 3 through 5 of the...
-
4
Use SQL Conversion Functions to Work with Different Data TypesUse SQL conversion functions to work with different data types. SQL server uses data types to store...
-
2
-
9
A ToString Replacement that Recognizes Different Data Types
-
3
Retrospective of multiple views to display at different times advertisements I have an application that should show one of a set of different views...
-
6
It's a small world after all, but BRIC economies have different views on tech's capabilities than the US and Europe By Stuart Lauc...
-
1
Arithmetic Mean: Meaning, Example, Types, Merits, and DemeritsA single value used to symbolise a whole set of data is called the Measure of Central Tendency. In comparison to other values, it is a ty...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK