July 25, 2015

Convert data to specific type in ABAP

There are occasions where we need to make specific conversions in our data. That scenario happened to me with a log table whose data needed to be used to make summaries. There was a column of CHAR type which had the values to be used.

Fortunately this table also had the table name and the field name as fields and they happened to be very useful for my purposes.

I found a function module named 'RS_CONV_EX_2_IN' where you send as parameters the value to be converted, the referenced table name and referenced field name and the result is the value converted to the specified format. The only caveat here is that you have to specify the appropriate table and the field name as parameters.

Let's code ABAP.

Copy and paste the following code and run:

report  zconversion.

start-of-selection.
  perform test.

form test.
  data: oref      type ref to cx_root,
        lv_value  type char255,
        lv_result type datum,
        lv_kbetr  type bwert,
        lv_error  type string.

  try.
    lv_value = '01012015'.
    perform convert_to_date using lv_value changing lv_result.
    write: / 'Date:', lv_result.
  catch cx_root into oref.
    lv_error = oref->get_text( ).
    write / lv_error.
  endtry.

  try.
    lv_value = '1033.12'.
    perform convert_to_curr using lv_value changing lv_kbetr.
    write: / 'Value:', lv_kbetr.
  catch cx_root into oref.
    lv_error = oref->get_text( ).
    write / lv_error.
  endtry.
endform.

form convert_to_date using p_value type char255 changing r_value type datum.
  perform convert using p_value 'SYST' 'DATUM' changing r_value.
endform.

form convert_to_curr using p_value type char255 changing r_value type bwert.
  perform convert using p_value 'KONP' 'KBETR' changing r_value.
endform.

form convert using p_value type char255 p_tabname type tabname p_fieldname type fieldname changing r_value type data.
  data wa_tabfield type tabfield.

  check p_value is not initial.

  wa_tabfield-tabname    = p_tabname.
  wa_tabfield-fieldname  = p_fieldname.

  call function 'RS_CONV_EX_2_IN'
    exporting
      input_external  = p_value
      table_field     = wa_tabfield
*     currency                           =
    importing
      output_internal = r_value
    exceptions
      input_not_numerical                = 1
      too_many_decimals                  = 2
      more_than_one_sign                 = 3
      ill_thousand_separator_dist        = 4
      too_many_digits                    = 5
      sign_for_unsigned                  = 6
      too_large                          = 7
      too_small                          = 8
      invalid_date_format                = 9
      invalid_date                       = 10
      invalid_time_format                = 11
      invalid_time                       = 12
      invalid_hex_digit                  = 13
      unexpected_error                   = 14
      invalid_fieldname                  = 15
      field_and_descr_incompatible       = 16
      input_too_long                     = 17
      no_decimals                        = 18
      invalid_float                      = 19
      conversion_exit_error              = 20
      others                             = 21.

  if sy-subrc <> 0.
* message id sy-msgid type sy-msgty number sy-msgno
*         with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  endif.
endform.

The most important part of this code is the 'convert' form which makes all the conversion. In case the function fails then result is going to be the initial value of the variable. Check the SY-SUBRC value in the latter case.

Another important part of the code is 'r_value' parameter which is of  'data' value that is a generic type that will return the appropriate variable.

Hope it helps.
Please Share it! :)

No comments :

Post a Comment