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
Other versions:
7.31 | 7.40 | 7.54
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 ABAP Dictionary type specified by dtype
. The result has the type dtype
. The following can be specified for dtype
:
- A data element. In this case, the optional addition PRESERVING TYPE can be specified. If this addition is specified, the built-in data type, the length of the operand and the number of decimal places, and the target data type must match exactly.
- A built-in data type in ABAP Dictionary The addition PRESERVING TYPE cannot be specified in this case.
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[(5)] | CUKY |
abap.curr(len,decimals) | CURR with length len and decimals decimal places |
abap.decfloat16[(16)] | DECFLOAT16 |
abap.decfloat34[(34)] | DECFLOAT34 |
abap.dats[(8)] | DATS |
abap.datn[(8)] | DATN |
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.timn[(6)] | TIMN |
abap.unit[(2 | 3)] |
abap.utclong[(27)] | UTCLONG |
The actual length of the result is defined when the CDS view is activated and is 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:
- A literal without a domain prefix
- 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
- A case distinction with CASE
- A nested cast expression
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 | DECFLOAT16 | DECFLOAT34 | FLTP | CHAR | SSTRING | NUMC | DATS | TIMS | DATN | TIMN | UTCLONG | ACCP | CLNT | LANG | UNIT | CUKY | RAW |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
INT1 | x | x | x | x | x | x | x | x | x | x | y | y | - | - | - | - | - | - | - | x | - | - | - | - |
INT2 | x | x | x | x | x | x | x | x | x | x | y | y | - | - | - | - | - | - | - | - | - | - | - | - |
INT4 | x | x | x | x | x | x | x | x | x | x | y | y | - | - | - | - | - | - | - | - | - | - | - | - |
INT8 | x | x | x | x | x | x | x | x | x | x | y | y | - | - | - | - | - | - | - | - | - | - | - | - |
DEC | x | x | x | x | x | x | x | x | x | x | y | y | - | - | - | - | - | - | - | - | - | - | - | - |
CURR | x | x | x | x | x | x | x | - | - | x | y | y | - | - | - | - | - | - | - | - | - | - | - | - |
QUAN | x | x | x | x | x | x | x | x | x | x | y | y | - | - | - | - | - | - | - | - | - | - | - | - |
DECFLOAT16 | x | x | x | x | x | - | x | x | x | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - |
DECFLOAT34 | x | x | x | x | x | - | x | x | x | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - |
FLTP | - | - | - | - | - | - | - | - | - | x | - | - | - | - | - | - | - | - | - | - | - | - | - | - |
CHAR | - | - | - | - | - | - | - | - | - | - | x | x | x | x | x | - | - | - | p | x | x | x | x | - |
SSTRING | - | - | - | - | - | - | - | - | - | - | x | x | x | x | x | - | - | - | p | x | x | x | x | - |
NUMC | y | y | y | y | x | x | x | x | x | x | x | x | z | z | z | - | - | - | p | z | - | - | - | - |
DATS | - | - | - | - | - | - | - | - | - | - | x | x | - | z | - | - | - | - | - | - | - | - | - | - |
TIMS | - | - | - | - | - | - | - | - | - | - | x | x | - | - | z | - | - | - | - | - | - | - | - | - |
DATN | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | x | - | - | - | - | - | - | - | - |
TIMN | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | x | - | - | - | - | - | - | - |
UTCLONG | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | x | - | - | - | - | - | - |
ACCP | - | - | - | - | - | - | - | - | - | - | z | z | z | - | - | - | - | - | p | - | - | - | - | - |
CLNT | - | - | - | - | - | - | - | - | - | - | d | d | - | - | - | - | - | - | - | p | - | - | - | - |
LANG | - | - | - | - | - | - | - | - | - | - | d | d | - | - | - | - | - | - | - | - | p | - | - | - |
UNIT | - | - | - | - | - | - | - | - | - | - | d | d | - | - | - | - | - | - | - | - | - | p | - | - |
CUKY | - | - | - | - | - | - | - | - | - | - | d | d | - | - | - | - | - | - | - | - | - | - | p | - |
RAW | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | p |
There are no further restrictions to note in combinations with "x". The following rules apply to the other combinations:
- In combinations with "y", the length of the target data type must be sufficient.
- In combinations with "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.
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
- If a data element is specified for
dtype
, the result of the expression is given its semantic attributes. An exception to this is the use of the CAST expression within a case distinction using CASE,
- The addition PRESERVING TYPE can be used to perform a cast to data elements. This is done solely to modify the semantic attributes of the operand.
- The characters in the surrogate area of the system code page UTF-16 are handled as two characters in cast expressions for strings. Care should be taken to avoid splitting these characters in cutoff operations.
- When performing a conversion between currency fields with type CURR, it should be noted that CAST respects the decimal places defined for the type. In ABAP applications, on the other hand, the position of the decimal point is usually ignored.
- A CAST expression should be platform-independent. Conversions from the type FLTP to other numeric types are not allowed because the result would be platform-dependent.
- An aggregate expression AVG has the type FLTP by default. It can be specified as an operand but it is not possible to cast to other types. Therefore there is a separate addition for AVG AS dtype.
- Special built-in conversion functions are available for special conversions that cannot be covered by a CAST expression:
- FLTP_TO_DEC for converting FLTP to packed numbers.
- The built-in numeric function CEIL can be used for conversions from DEC to INT4 that are not possible with a CAST expression. Here, it is usually necessary to modify the result.
Example
Cast 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 }
Example
In the following view, the column char1 of the database table 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.
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view demo_cds_cast_data_element
as select from
demo_expressions
{
cast ( char1 as DEMO_CHAR_TEXT preserving type) as char_with_text
};
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.
CALL FUNCTION 'DDIF_FIELDINFO_GET'
EXPORTING
tabname = 'DEMO_CDS_CAST_DE'
fieldname = 'CHAR_WITH_TEXT'
langu = sy-langu
TABLES
dfies_tab = dfies_tab.
cl_demo_output=>display( dfies_tab[ 1 ]-fieldtext ).
Example
In the following view, a literal is given the technical and semantic attributes of the data element S_MANDT.
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view demo_cds_cast_clnt
as select from
scarr
{
key cast ( 'XXX' as s_mandt )
as pseudo_client,
key carrid,
carrname
};