SAP NetWeaver AS ABAP Release 751, ©Copyright 2017 SAP AG. All rights reserved.
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 - cast_expr
Syntax
... CAST( operand AS dtype [PRESERVING TYPE]) ...
Effect
Casting in a SELECT statement of a CDS view. The cast expression converts the value of the operand operand to the dictionary type specified by dtype. The result has the type dtype. The following can be specified for dtype:
The following table shows the syntax for specifying built-in data types:
dtype | Dictionary Type |
abap.char( len ) | CHAR with length len |
abap.clnt[(3)] | CLNT |
abap.cuky( len ) | CHAR with length len |
abap.curr(len,decimals) | CURR with length len and decimals decimal places |
abap.dats[(8)] | DATS |
abap.dec(len,decimals) | DEC with length len and decimals decimal places |
abap.fltp[(16,16)] | FLTP |
abap.int1[(3)] | INT1 |
abap.int2[(5)] | INT2 |
abap.int4[(10)] | INT4 |
abap.int8[(19)] | INT8 |
abap.lang[(1)] | LANG |
abap.numc( len ) | NUMC with length len |
abap.quan(len,decimals) | QUAN with length len with decimals decimal places |
abap.raw(len) | RAW |
abap.sstring(len) | SSTRING |
abap.tims[(6)] | TIMS |
abap.unit( len ) | CHAR with length len |
The actual length of the result is defined when the CDS view is activated and is must be at least as long as an explicitly defined length len. The predefined values can be specified for types with fixed lengths and decimal places, but this is not mandatory.
The following can be specified for operand:
Cast expressions can be specified in the SELECT list and in operand positions of expressions.
The following table shows which combinations of built-in data types in ABAP Dictionary can currently be cast to each other and what the prerequisites are in each case. There is a special list of conversion rules for every combination.
from/to | INT1 | INT2 | INT4 | INT8 | DEC | CURR | QUAN | FLTP | CHAR | SSTRING | NUMC | DATS | TIMS | ACCP | CLNT | LANG | UNIT | CUKY | RAW |
INT1 | x | x | x | x | y | y | y | x | y | y | - | - | - | - | x | - | - | - | - |
INT2 | x | x | x | x | y | y | y | x | y | y | - | - | - | - | - | - | - | - | - |
INT4 | x | x | x | x | y | y | y | x | y | y | - | - | - | - | - | - | - | - | - |
INT8 | x | x | x | x | y | y | y | x | y | y | - | - | - | - | - | - | - | - | - |
DEC | - | - | - | - | x | x | x | x | y | y | - | - | - | - | - | - | - | - | - |
CURR | - | - | - | - | x | x | x | x | y | y | - | - | - | - | - | - | - | - | - |
QUAN | - | - | - | - | x | x | x | x | y | y | - | - | - | - | - | - | - | - | - |
FLTP | - | - | - | - | - | - | - | x | - | - | - | - | - | - | - | - | - | - | - |
CHAR | - | - | - | - | - | - | - | - | x | x | y | y | y | p | y | y | y | y | - |
SSTRING | - | - | - | - | - | - | - | - | x | x | y | y | y | p | y | y | y | y | - |
NUMC | y | y | y | y | x | x | y | x | y | y | z | z | z | p | z | - | - | - | - |
DATS | - | - | - | - | - | - | - | - | y | y | - | z | - | - | - | - | - | - | - |
TIMS | - | - | - | - | - | - | - | - | y | y | - | - | z | - | - | - | - | - | - |
ACCP | - | - | - | - | - | - | - | - | z | z | z | - | - | p | - | - | - | - | - |
CLNT | - | - | - | - | - | - | - | - | d | d | - | - | - | - | p | - | - | - | - |
LANG | - | - | - | - | - | - | - | - | d | d | - | - | - | - | - | p | - | - | - |
UNIT | - | - | - | - | - | - | - | - | d | d | - | - | - | - | - | - | p | - | - |
CUKY | - | - | - | - | - | - | - | - | d | d | - | - | - | - | - | - | - | p | - |
RAW | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | p |
If a built-in data type from ABAP Dictionary is specified for dtype, no further restrictions apply to combinations with "x". The following rules apply to the other combinations:
In the case of incompatible types, the content of the operand is converted to the target type (exceptions can be raised if values are not suitable). In compatible types, a syntax check warning occurs (unless the target data type is specified as a data element using the addition PRESERVING TYPE).
Notes
Example
Cast expressions in a SELECT list.
Example
In the following view, the column char1 of the database DEMO_EXPRESSIONS is cast to the data element demo_char_text with the same technical attributes. In this case, it is advisable to specify the addition PRESERVING TYPE.
The following function module call returns the attributes of the view field The text shows that the semantic attributes of the data element were applied. The column char1 does not have its own text.
Example
In the following view, a literal is given the technical and semantic attributes of the data element S_MANDT.