Skip to content

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, Predefined Functions →  ABAP CDS - Special Features 

ABAP CDS - Conversion Functions for Currencies and Units

Other versions: 7.31 | 7.40 | 7.54

Syntax

... CURRENCY_CONVERSION( p1 => a1, p2 => a2, ... )
  | UNIT_CONVERSION( p1 => a1, p2 => a2, ... )
  | DECIMAL_SHIFT( p1 => a1, p2 => a2, ... ) ...

Variants

1. ... CURRENCY_CONVERSION( p1 => a1, p2 => a2, ... )

2. ... UNIT_CONVERSION( p1 => a1, p2 => a2, ... )

3. ... DECIMAL_SHIFT( p1 => a1, p2 => a2, ... )

Effect

Functions for conversions between currencies and units. The functions have keyword parameters p1, p2, ..., to which the actual parameters a1, a2, ... must be assigned when called using =>.

Variant 1

... CURRENCY_CONVERSION( p1 => a1, p2 => a2, ... )

Effect

Function CURRENCY_CONVERSION performs a currency conversion for the value passed to formal parameter amount. The result has the data type CURR with the same technical properties as the actual parameter passed to amount. The currency conversion is performed on the basis of the rules stored in the database tables TCUR... of package SFIB. These rules can be edited using transaction OB08.

The table below shows the actual parameters p1, p2, ... and their meaning.

Formal Parameter Optional Meaning Data Type Actual Parameter
amount - Initial value CURR Field of a data source
source_currency - Source currency from column WAERS of database table TCURC CUKY Field of a data source, literal
target_currency - Target currency from column WAERS of database table TCURC CUKY Field of a data source,literal, parameter
exchange_rate_date - Exchange rate date from column WAERS of database table TCURR DATS Literal, parameter
exchange_rate_type X Exchange rate type from column KURST of database table TCURR, default value: "M" CHAR with length 4 Literal, parameter
client X Client whose data is used for the currency conversion; default value: content of sy-mandt CLNT Field of a data source,literal, parameter
round X If "X" (default value), the intermediate result of the conversion is rounded to the end result using commercial rounding; otherwise, it is cut CHAR with length 1 Literal, parameter
DECIMAL_SHIFT X If "X" (default value), the source value is transformed, before the conversion, from the internal SAP storage format without decimal places into a number with decimal places. CHAR with length 1 Literal, parameter
DECIMAL_SHIFT_back X If "X" (default value), the result is transformed, after the conversion, from a number with decimal places to the internal SAP storage format without decimal places. CHAR with length 1 Literal, parameter
error_handling X Error handling. If "FAIL_ON_ERROR" (default value), an error raises an exception; if "SET_TO_NULL", the result is reset to thenull value; if "KEEP_UNCONVERTED", the source value is not changed. CHAR with length 20 Literal, parameter


Example

The following CDS view calls a currency conversion in the SELECT list for column AMOUNT of database table DEMO_PRICES. The target currency must be passed as a parameter. In the event of an error, for example when a currency does not exist, the result is reset to zero.

@AbapCatalog.sqlViewName: 'DEMO_CDS_CRRCONV'
  define view demo_cds_currency_conversion
   with parameters to_currency:abap.cuky(5),
                   exc_date:abap.dats
   as select from demo_prices
   { id,
     currency_conversion( amount => amount,
                          source_currency => currency,
                          round => 'X',
                          target_currency => :to_currency,
                          exchange_rate_date => :exc_date,
                          error_handling => 'SET_TO_NULL' ) as amount,
     :to_currency as currency }

Program DEMO_CDS_CURRENCY_CONVERSION accesses the view in a SELECT statement, in which the target currency is passed. As prerequisite for the example, the currencies and conversion rules must be available in the corresponding database tables.

Variant 2

... UNIT_CONVERSION( p1 => a1, p2 => a2, ... )

Effect

Function UNIT_CONVERSION performs a unit conversion for the value passed to formal parameter quantity. The result has the same data type as the actual parameter passed to quantity. The unit conversion is performed on the basis of the rules stored in transaction CUNI and in the database tables T006... of package SZME.

The table below shows the actual parameters p1, p2, ... and their meaning.

Formal Parameter Optional Meaning Data Type Actual Parameter
quantity - Initial value QUAN,DEC,INT1,INT2, INT4, FLTP Field of a data source
source_unit - Source currency from column MSEHI of database table T006 UNIT Field of a data source, literal
target_unit - Target unit from column MSEHI of database table T006 UNIT Field of a data source,literal, parameter
client X Client whose data is used for the unit conversion; default value: content of sy-mandt CLNT Field of a data source,literal, parameter
error_handling X Error handling. If "FAIL_ON_ERROR" (default value), an error raises an exception; if "SET_TO_NULL", the result is reset to thenull value; if "KEEP_UNCONVERTED", the source value is not changed. CHAR with length 20 Literal, parameter


Note

The precision of the result of the unit conversion depends on the database platform. The highest precision is achieved on databases that support decimal floating point numbers.


Example

The following CDS view calls a unit conversion in the SELECT list for column DEC3 of database table DEMO_EXPRESSIONS. The source unit is a literal that is cast to the required type. The target unit must be passed as a parameter. In the event of an error, for example if a conversion between the entered units is impossible, the result is reset to zero.

@AbapCatalog.sqlViewName: 'DEMO_CDS_UNTCNV'
  define view demo_cds_unit_conversion
   with parameters to_unit:abap.unit(3)
   as select from demo_expressions
   { id,
     dec3 as original_value,
     cast( 'MI' as abap.unit(3) ) as original_unit,
     unit_conversion( quantity => dec3,
                      source_unit => cast( 'MI' as abap.unit(3) ),
                      target_unit => :to_unit,
                      error_handling => 'SET_TO_NULL' ) as converted_value,
     :to_unit as converted_unit }

Program DEMO_CDS_UNIT_CONVERSION accesses the view in a SELECT statement, in which the source and target units are passed. As prerequisite for the example, the units and their conversion rules must be available in the corresponding database tables.

Variant 3

... DECIMAL_SHIFT( p1 => a1, p2 => a2, ... )

Effect

Function DECIMAL_SHIFT sets the decimal separator of the value that is passed to formal parameter amount according to a currency. The result has the data type CURR with the same technical properties as the actual parameter passed to amount. Possible currencies and their decimal places are based on the database tables TCUR... of package SFIB.

Formal Parameter Optional Meaning Data Type Actual Parameter
amount - Source value; digits are interpreted without decimal separators CURR Field of a data source
currency - Currency from column WAERS of database table TCURC; the related decimal places are determined from column CURRDEC of TCURX CUKY Field of a data source, literal
error_handling X Error handling. If "FAIL_ON_ERROR" (default value), an error raises an exception; if "SET_TO_NULL", the result is reset to thenull value; if "KEEP_UNCONVERTED", the source value is not changed. CHAR with length 20 Literal, parameter


Example

In the SELECT list, the following CDS view sets the decimal separator for column AMOUNT of database table DEMO_PRICES to between 0 and 5, according to the relevant currency. In error cases, for example if a currency does not exist, the result is set to zero.

@AbapCatalog.sqlViewName: 'DEMO_CDS_DCSHFT'
  define view demo_cds_decimal_shift
   as select from demo_prices
   { id,
     amount as original,
     decimal_shift( amount => amount,
                    currency => cast( '0 ' as abap.cuky(5) ),
                    error_handling => 'SET_TO_NULL' ) as shift_0,
     decimal_shift( amount => amount,
                    currency => cast( '1 ' as abap.cuky(5) ),
                    error_handling => 'SET_TO_NULL' ) as shift_1,
     decimal_shift( amount => amount,
                    currency => cast( '2 ' as abap.cuky(5) ),
                    error_handling => 'SET_TO_NULL' ) as shift_2,
     decimal_shift( amount => amount,
                    currency => cast( '3 ' as abap.cuky(5) ),
                    error_handling => 'SET_TO_NULL' ) as shift_3,
     decimal_shift( amount => amount,
                    currency => cast( '4 ' as abap.cuky(5) ),
                    error_handling => 'SET_TO_NULL' ) as shift_4,
     decimal_shift( amount => amount,
                    currency => cast( '5 ' as abap.cuky(5) ),
                    error_handling => 'SET_TO_NULL' ) as shift_5 }

The program DEMO_CDS_DECIMAL_SHIFT accesses the view in a SELECT statement. Since the result of the function always has two decimal places, currencies with three decimal places or more are rounded as required. As prerequisite for the example, the currencies and their decimal places rules must be available in the corresponding database tables.