ABAP Keyword Documentation → ABAP Dictionary → ABAP CDS in ABAP Dictionary → ABAP CDS - Views → ABAP CDS - DDL Statements → ABAP CDS - DEFINE VIEW → ABAP CDS - SELECT → ABAP CDS - SELECT, Operands and Expressions
ABAP CDS - cast_expr
Other versions:
7.31 | 7.40 | 7.54
Syntax
... cast( operand AS dtype ) ...
Effect
Casting (type adjustment) in a SELECT statement of a CDS view in
ABAP CDS. This casting 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
:
dtype | Dictionary Type |
---|---|
abap.char( len ) | CHAR with length len |
abap.clnt | CLNT |
abap.cuky( len ) | CHAR with length len |
abap.curr( len, decimals ) | DEC with length len and decimals decimal places |
abap.dats | DATS |
abap.dec( len , decimals ) | DEC with length len and decimals decimal places |
abap.fltp | FLTP |
abap.int1 | INT1 |
abap.int2 | INT2 |
abap.int4 | INT4 |
abap.lang | LANG |
abap.numc( len ) | NUMC with length len |
abap.quan( len , decimals ) | DEC with length len and decimals decimal places |
abap.tims | 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 following can be specified for operand:
- A literal
- A field of a data source data_source of the current CDS view
- A path expression that identifies a field of a data source data_source
Casting expressions cannot be nested and cannot contain case distinctions as operands. Casting expressions can be specified in the SELECT list and in operand positions of other expressions.
The following tables shows which data types can currently be cast to which other data types:
from/to | INT1 | INT2 | INT4 | CHAR | DEC | NUMC | CLNT | CURR | DATS | FLTP | LANG | QUAN | SSTR | TIMS | UNIT | CUKY |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
INT1 | x | x | x | y | y | - | x | y | - | x | - | y | - | - | - | - |
INT2 | x | x | x | y | y | - | - | y | - | x | - | y | - | - | - | - |
INT4 | x | x | x | y | y | - | - | y | - | x | - | y | - | - | - | - |
NUMC | y | y | y | y | x | x | z | x | z | x | - | y | - | z | - | - |
CHAR | - | - | - | x | - | y | y | - | y | - | y | - | - | y | y | y |
DEC | - | - | - | y | y | - | - | y | - | x | - | y | - | - | - | - |
QUAN | - | - | - | y | y | - | - | y | - | x | - | x | - | - | - | - |
CURR | - | - | - | y | y | - | - | x | - | x | - | y | - | - | - | - |
FLTP | - | - | - | - | - | - | - | - | - | x | - | - | - | - | - | - |
In combinations using "y", the target data type must be long enough and in combinations using "z", the lengths of the data types must match exactly.
Example
Casting expressions in a SELECT list.
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 as original_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 }