SAP CDS CAST EXPRESSION V2



Get Example source ABAP code based on a different SAP table
  


• PRESERVING TYPE ABAP_CDS_EXPRESSION
• AS ABAP_CDS_EXPRESSION
• CAST ABAP_CDS_EXPRESSION

ABAP_CDS_DDL - CDS View Entity, cast_expr

ABAP_SYNTAX
... CAST( operand AS dtype $[PRESERVING TYPE$]) ...

What does it do?
Casting in a SELECT statement of a CDS view entity. The cast expression converts the value of the operand operand to the type specified by dtype. The result has the type dtype.
The following can be specified for dtype:
A CDS simple type. In this case, the optional addition PRESERVING TYPE can be specified.
A DDIC 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.sstring(len)SSTRING
abap.tims$[(6)$]TIMS
abap.timn$[(6)$]TIMN
abap.unit$[(2$|3)$]UNIT with length 2 or 3 (standard length)
abap.utclong$[(27)$]UTCLONG
The actual length of the result is defined when the CDS view entity is activated and is at least as long as an explicitly defined length len. It is possible, but not mandatory, to specify the length for types with fixed lengths and decimal places.
The following can be specified for operand:
A literal
A parameter
A session variable
A field of a data source data_source of the current CDS view entity
An aggregate expression
A path expression that identifies a field of a data source data_source
A built-in function
An SQL-based scalar function
An arithmetic expression
A case distinction
Another, 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/toINT1INT2INT4 INT8DECCURR QUANDECFLOAT16DECFLOAT34 FLTPCHARSSTRINGNUMC DATSTIMSDATN TIMNUTCLONGACCPCLNT LANGUNITCUKY RAWDF16_DECDF34_DEC
INT1xxxxxxxxxxyy------ -x------
INT2xxxxxxxxxxyy------ --------
INT4xxxxxxxxxxyy------ --------
INT8xxxxxxxxxxyy------ --------
DECxxxxxxxxxxyy------ --------
CURRxxxxxxx--xyy------ --------
QUANxxxxxxxxxxyy------ --------
DECFLOAT16xxxxxxxxx-x----- ----------
DECFLOAT34xxxxxxxxx-x----- ----------
DF16_DEC-------xx------- ---- ----d-
DF34_DEC-------xx------- -- -------d
FLTPxxxxxxxxxx-------- --------
CHARxxxxxxxxx-xxxxx--- pxxxx---
SSTRINGxxxxxxxxx-xxxxx-- -pxxxx---
NUMCyyyyxxxxxxxxzzz--- pz------
DATSxxxxxxxxx-xx-z---- --------
TIMSxxxxxxxxx-xx--z--- --------
DATN---------------x-- --------
TIMN----------------x- --------
UTCLONG----------------- x--------
ACCP----------zzz----- p-------
CLNT----------dd------ -p------
LANG----------dd------ --p-----
UNIT----------dd------ ---p----
CUKY----------dd------ ----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, a simple type or a data element must be specified as the target data type. It is not possible to specify a built-in data type from ABAP Dictionary.
In combinations with d, the simple type or the data element can have a suitable target type in accordance with the table above and with any length.
In combinations with p, the simple type or 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). For compatible types, a syntax check warning occurs (unless the target data type is specified as a simple type or as a data element using the addition PRESERVING TYPE).
If the operand of a cast expression has the null value, the result of the expression is also the null value.
If the target type dtype is specified as CURR or QUAN , then a Semantics reference annotation specifying a currency key or unit key is mandatory.



Latest notes:

If a CDS simple type is specified for dtype, the result of the expression inherits the semantic properties of the simple type.
If a data element is specified for dtype, the result of the expression inherits the semantic properties of the data element. An exception to this is the use of the CAST expression within a case distinction.
If operand and dtype have the same data type and the same length, a syntax check warning occurs. This syntax check warning can be suppressed using the addition PRESERVING TYPE. If dtype is a simple type or a data element and the addition PRESERVING TYPE is not available, the warning can be ignored. It is currently not possible to suppress the warning.
If the addition PRESERVING TYPE is specified, the data type of the operand stays the same and only the semantic properties are changed.
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 truncation 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.
Special built-in conversion functions are available for conversions that cannot be covered by a CAST expression:
FLTP_TO_DEC for converting FLTP to packed numbers.
BINTOHEX and HEXTOBIN for conversions of byte strings to character strings and back.
Conversion functions for units and currencies
Conversion functions for dates, times, and time stamps
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
Cast expressions in a SELECT list.
DDLS DEMO_SALES_CDS_SO_CAST
ABAP_EXAMPLE_END

ABAP_EXAMPLE_VX5
In the following view entity, the column char1 of the DDIC database table DEMO_EXPRESSIONS is cast to the simple type DEMO_BT_CHAR_TEXT with the same technical properties. In this case, it is advisable to specify the addition PRESERVING TYPE.
DDLS DEMO_CDS_CAST_SIMPLE_TYPE_VE
ABAP_EXAMPLE_END

ABAP_EXAMPLE_VX5
In the following view, a literal is given the technical and semantic properties of the simple type DEMO_BT_MANDT.
DDLS DEMO_CDS_CAST_CLNT_VE
ABAP_EXAMPLE_END