Skip to Content
Technical Articles
Author's profile photo Safa Golrokh Bahoosh

New currency conversion function in ABAP SQL

Currency conversions are now possible in ABAP SQL as well as ABAP CDS. From 7.55 release, the new currency conversion function for converting between unit currencies in an ABAP SQL statement is supported as

…CURRENCY_CONVERSION( p1 = a1, p2 = a2, … )…

Which is simillar to currency conversion function for ABAP CDS

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

with keyword parameters p1, p2, … (some of which are optional), to which actual parameters a1, a2, … must be assigned when a function is called. The table below shows the parameters p1, p2, … and their meaning.

Parameter

Optional

Meaning
amount Inbound value.
source_currency Source currency from column WAERS of the DDIC database table TCURC.
target_currency Target currency from column WAERS of the DDIC database table TCURC.
exchange_rate_date Exchange rate date for column GDATU of the DDIC database table TCURR.
exchange_rate_type x Exchange rate type from column KURST of the DDIC database table TCURR, default value: “M”.
client x Client whose rules are used to perform the currency conversion.
Default: Content of the client column of the current row.
round x The intermediate result of the conversion is rounded to the end result using commercial rounding; otherwise, it is truncated.
decimal_shift x The decimal places of the source value are moved as specified by the decimal places of the source currency (see below).
decimal_shift_back x The decimal places of the result are moved as specified by the decimal places of the target currency (see below).
on_error x Error handling.

The function CURRENCY_CONVERSION performs a currency conversion for the value passed to the formal parameter amount. The result has the data type CURR with the same technical attributes as the actual parameter passed to amount. The value passed is rounded to two decimal places before it is converted. The conversion is performed on the database, which means that part of the calculation takes place using different rounding rules from ABAP. No matter how the conversion is made, the same results cannot be expected as when using standard function modules for currency conversion, since these modules are generally less precise and round the intermediate results accordingly. After the conversion, the result is divided by 10 to the power of the number of decimal places of the target currency.

Example

The following is an excerpt of the program DEMO_ASQL_CURRENCY_CONVERSION. The SELECT statement calls a currency conversion in its SELECT list for the column AMOUNT of the DDIC database table DEMO_PRICES. The target currency is passed as a host variable. In the event of an error, for example when a currency is not available, an exception is raised. As a prerequisite for the example, the currencies and conversion rules must be available in the corresponding DDIC database tables.

DATA currency TYPE c LENGTH 5 VALUE 'USD'.

SELECT FROM demo_prices
       FIELDS id,
              amount,
              currency,
                currency_conversion(
                amount = amount,
                source_currency = currency,
                target_currency = @currency,
                exchange_rate_date = @sy-datlo,
                round = 'X',
                on_error = @sql_currency_conversion=>c_on_error-fail )
                    AS Converted_Amount,
                @currency AS converted_currency
        ORDER BY id
          INTO TABLE @DATA(converted_prices_asql).

The output is

ID Amount Currency Converted Amount Converted Currency
1 5100.0 EUR 5610.0  USD
2 1.0 GBP 1.5  USD
3 1.0 JPY 0.87  USD
4 1.0 USD 1.0  USD

As a comparison, the same conversion is also performed using the function module CONVERT_TO_LOCAL_CURRENCY.

Example

The following CDS view entity performs a currency conversion in the SELECT list for the column AMOUNT of the DDIC database table DEMO_PRICES. The target currency must be passed as a parameter. Using the same input, the output is similar as the above example.

@AccessControl.authorizationCheck: #NOT_REQUIRED 
define view entity DEMO_CDS_CURR_CONV
  with parameters 
    to_currency :abap.cuky( 5 ), 
    exc_date    :abap.dats 
  as select from 
    demo_prices 
    { 
      key id, 
       amount,
       currency,     
      @Semantics.amount.currencyCode: 'currency' 
      currency_conversion( amount => amount, 
                          source_currency => currency, 
                          round =>  'X', 
                          target_currency => $parameters.to_currency, 
                          exchange_rate_date => $parameters.exc_date, 
                          error_handling => 'SET_TO_NULL' ) as ConvertedAmount, 
      $parameters.to_currency                               as ConvertedCurrency 
    }

To know more details on handling the currency conversions, its roles and limitations, please check the ABAP Keyword Documentation.

Assigned tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.