AS ABAP Release 753, ©Copyright 2019 SAP AG. All rights reserved.
ABAP Keyword Documentation → ABAP − Reference → Processing External Data → ABAP Database Access → ABAP SQL → ABAP SQL - Operands and Expressions → ABAP SQL - SQL Expressions sql_exp →sql_exp - sql_cast
Syntax
... CAST( operand AS dtype ) ...
Effect
Type adjustment in ABAP SQL. A cast expression converts the value of the operand operand to the dictionary type specified by dtype. SQL expressions, which have a type that matches dtype (as shown below), can be used as an operand operand. The result is a representation of the source value in type dtype.
Built-in data types in ABAP Dictionary with the following syntax can be specified for dtype:
len and decimals can be used to specify lengths and decimal places for those dictionary types that have variable lengths and decimal places. Literals or constant host variables of the ABAP type b, s, i, or int8 can be specified for len and decimals. If no lengths or decimal places are specified for these types, these values are taken from the operand operand. In both cases, the lengths and decimal places must meet the requirements of the following table.
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 | NUMC | DATS | TIMS | CLNT | LANG | UNIT | CUKY | SSTRING | RAW |
INT1 | x | x | x | x | x | x | x | x | z | - | - | - | - | - | - | - | - | - |
INT2 | x | x | x | x | x | x | x | x | z | - | - | - | - | - | - | - | - | - |
INT4 | x | x | x | x | x | x | x | x | z | - | - | - | - | - | - | - | - | - |
INT8 | x | x | x | x | x | x | x | x | z | - | - | - | - | - | - | - | - | - |
DEC | z | z | z | z | x | x | x | x | z | - | - | - | - | - | - | - | - | - |
CURR | - | - | - | - | x | x | x | x | z | - | - | - | - | - | - | - | - | - |
QUAN | - | - | - | - | x | x | x | x | z | - | - | - | - | - | - | - | - | - |
FLTP | - | - | - | - | - | - | - | x | - | - | - | - | - | - | - | - | - | - |
CHAR | - | - | - | - | - | - | - | - | x | y | y | y | y | y | x | y | - | - |
NUMC | x | x | x | x | x | x | x | x | y | y | y | y | y | - | - | - | - | - |
DATS | - | - | - | - | - | - | - | - | x | y | x | - | - | - | - | - | - | - |
TIMS | - | - | - | - | - | - | - | - | x | y | - | x | - | - | - | - | - | - |
CLNT | - | - | - | - | - | - | - | - | x | y | - | - | x | - | - | - | - | - |
LANG | - | - | - | - | - | - | - | - | x | - | - | - | - | x | - | - | - | - |
UNIT | - | - | - | - | - | - | - | - | x | - | - | - | - | - | x | - | - | - |
CUKY | - | - | - | - | - | - | - | - | x | - | - | - | - | - | - | x | - | - |
SSTRING | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | y | - |
RAW | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | c |
There are no further restrictions to note in combinations using "x". The following prerequisites 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). If the operand of the expression has the null value, the result of the expression is also the null value.
Notes
Example
In the following SELECT list, a time stamp of the type DEC with length 15 is split into its date part and time part using different methods. Various CAST expressions are needed here to create a suitable type in every operand position.
GET TIME STAMP FIELD DATA(timestamp).
DELETE FROM demo_expressions.
INSERT demo_expressions FROM @(
VALUE #( id = 'X' timestamp1 = timestamp ) ).
SELECT SINGLE
FROM demo_expressions
FIELDS CAST( CAST( DIV( timestamp1 , 1000000 )
AS CHAR )
AS DATS ) AS date,
CAST( SUBSTRING( CAST( timestamp1
AS CHAR ), 9, 6 )
AS TIMS ) AS time
INTO @DATA(wa).
cl_demo_output=>display( wa ).
Executable Example
SQL Expressions, Cast Expression