Skip to content

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

Other versions: 7.31 | 7.40 | 7.54

Syntax


... CAST( sql_exp AS dtype ) ...

Effect

Type adjustment in ABAP SQL. A cast expression converts the value of the operand sql_exp 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 sql_exp. 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:

  • Numeric types
  • INT1
  • INT2
  • INT4
  • INT8
  • DEC[( len[, decimals] )]
  • D16N for DECFLOAT16
  • D34N for DECFLOAT34
  • FLTP
  • Character-like types
  • CHAR[( len )]
  • SSTRING[( len )]
  • Byte-like types
  • RAW[( len )]
  • Date types/time types
  • DATS
  • TIMS
  • DATN
  • TIMN
  • UTCL for UTCLONG
  • Character-like types with special semantics
  • NUMC[( len )]
  • CLNT
  • LANG
  • Currency fields and quantity fields
  • CURR[( len[, decimals] )]
  • CUKY
  • QUAN[( len[, decimals] )]
  • UNIT( 2|3 )

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 host constants 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 result of the specified SQL expression sql_exp. 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 DECFLOAT16 DECFLOAT34 FLTP CHAR SSTRING NUMC DATS TIMS DATN TIMN UTCLONG CLNT LANG UNIT CUKY RAW
INT1 x x x x x x x x x x z z - - - - - - - - - -
INT2 x x x x x x x x x x z z - - - - - - - - - - -
INT4 x x x x x x x x x x z z - - - - - - - - - - -
INT8 x x x x x x x x x x z z - - - - - - - - - - -
DEC x x x x x x x x x x z z - - - - - - - - - - -
CURR x x x x x x x x x x z z - - - - - - - - - - -
QUAN x x x x x x x x x x z z - - - - - - - - - - -
DECFLOAT16 x x x x x x x x x x z z - - - - - - - - - - -
DECFLOAT34 x x x x x x x x x x z z - - - - - - - - - - -
DF16_DEC x x x x - - - x x - - - - - - - - - - - - - -
DF34_DEC x x x x - - - x x - - - - - - - - - - - - - -
FLTP - - - - - - - - - x - - - - - - - - - - - - -
CHAR - - - - - - - - - - x x y y y - - - y y c y -
SSTRING - - - - - - - - - - x x y y y - - - y y c y -
NUMC x x x x x x x x x x y y y y y - - - y - - - -
DATS - - - - - - - - - - x x y x - x - - - - - - -
TIMS - - - - - - - - - - x x y - x - x - - - - - -
DATN - - - - - - - - - - - - - - - x - - - - - - -
TIMN - - - - - - - - - - - - - - - - x - - - - - -
UTCLONG - - - - - - - - - - - - - - - - - x - - - - -
CLNT - - - - - - - - - - x x y - - - - - x - - - -
LANG - - - - - - - - - - x x - - - - - - - x - - -
UNIT - - - - - - - - - - x x - - - - - - - - x - -
CUKY - - - - - - - - - - x x - - - - - - - - - x -
RAW - - - - - - - - - - - - - - - - - - - - - - c

There are no further restrictions to note in combinations using "x". The following prerequisites apply to the other combinations:

  • For combinations with "y", the initial length cannot be less than the target length.
  • For combinations with "z", the target length must be sufficient to represent the value in the source.
  • For combinations with "c", the initial length and target length must be equal.

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

  • Specifying a cast expression always means specifying an SQL expression. Cast expressions can only be specified for operand positions for which SQL expressions are possible.

  • Type modifications can be used in operand positions of ABAP SQL statements to convert operands without a suitable type to an operand with a suitable type.

  • 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.

  • For the missing combinations of type modifications that cannot be covered by a CAST expression, construction operators such as CONV can be used in host expressions (at least for literals and host variables). In addition, built-in conversion functions exist for special conversions:

  • BINTOHEX and HEXTOBIN for conversions of byte strings to character strings and back.

  • If SQL expressions other than operands are used in a cast expression or if a data type other than FLTP is specified as dtype, the syntax check is run in strict mode from Release 7.50. This mode handles the statement more strictly than the normal syntax check.

Example

The following SELECT list splits a time stamp in a packed number into its date part and its time part. Various CAST expressions are needed here to create a suitable type in every operand position.

DATA(timestamp) = 
  cl_abap_tstmp=>utclong2tstmp_short( utclong_current( ) ). 
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

Continue

sql_exp - sql_cast, Conversion Rules

SQL Expressions, Cast Expression