Technology Blog Posts by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Surendra_Karma
Explorer
4,122

Introduction

Data type conversions are fundamental in any programming language, and SAP ABAP is no exception. Whether you're converting strings to integers, handling dates, or managing packed numbers, understanding these conversions can enhance the robustness and accuracy of your ABAP applications. In this blog post, I'll show 30 examples of data type conversions in ABAP—some might seem obvious, but they often come with unexpected challenges and exceptions.

Data Type Conversions in ABAP

Below, I'll walk you through a series of data type conversions using a simple ABAP program. The program demonstrates different scenarios, including handling exceptions that arise during conversions.

1. Normal String

REPORT ztest_sk. 
DATA
lv_int     TYPE i,
      lv_date    TYPE d,
      lv_time    TYPE t,
      lv_numc(3TYPE n,
      lv_str     TYPE string,
      lv_char    TYPE LENGTH 3,
      lv_pack    TYPE p LENGTH DECIMALS 2,
      lv_utc     TYPE utclong.
 

lv_str = 'ABCD12.3'.
WRITE /: '01. String: '. WRITE lv_str.

2. String to Character

lv_char = lv_str.  
WRITE /: '02. String to Character3: '. WRITE lv_char.  

Note: Data loss can occur when converting strings to smaller character fields, as shown in this example.

3. Exception: String to Character with EXACT #()

TRY.  
    lv_char = EXACT #( lv_str ).  
    WRITE /: lv_char.  
  CATCH cx_sy_conversion_data_loss.  
    WRITE /: '03. String to Character3 w/ EXACT#: CX_SY_CONVERSION_DATA_LOSS exception'.  
ENDTRY.  

Insight: Using EXACT #() with string to character conversion can result in exceptions if data loss is detected.

4. Alphanumeric String to Integer

TRY.  
    lv_int = lv_str.  
    WRITE /: lv_int.  
  CATCH cx_sy_conversion_no_number.  
    WRITE /: '04. String to integer: CX_SY_CONVERSION_NO_NUMBER exception'.  
ENDTRY.  

Challenge: Converting non-numeric strings to integers triggers exception.

5. String to Date

lv_date = lv_str.  
WRITE /: '05. String to date: '. WRITE lv_date.  

6. Exception: String to Date with EXACT #()

TRY.  
    lv_date = EXACT #( lv_str ).  
    WRITE /: lv_date.  
  CATCH cx_sy_conversion_no_date.  
    WRITE /: '06. String to date w/ EXACT#: CX_SY_CONVERSION_NO_DATE exception'.  
ENDTRY.  

7. String to Time

lv_time = lv_str.  
WRITE /: '07. String to time: '. WRITE lv_time.  

8. Exception: String to Time with EXACT #()

TRY.  
    lv_time = EXACT #( lv_str ).  
    WRITE /: lv_time.  
  CATCH cx_sy_conversion_no_time.  
    WRITE /: '08. String to time w/ EXACT#: CX_SY_CONVERSION_NO_TIME exception'.  
ENDTRY.  

9. String to Number

lv_numc = lv_str.  
WRITE /: '09. String to number3: '. WRITE lv_numc.  

10. Exception: String to Number with EXACT #()

TRY.  
    lv_numc = EXACT #( lv_str ).  
    WRITE /: lv_numc.  
  CATCH cx_sy_conversion_no_number.  
    WRITE /: '10. String to number3 w/ EXACT#: CX_SY_CONVERSION_NO_NUMBER exception'.  
ENDTRY.  

11. Exception: String to Packed Number

TRY.  
    lv_pack = lv_str.  
    WRITE /: lv_pack.  
  CATCH cx_sy_conversion_no_number.  
    WRITE /: '11. String to Packed5_2: CX_SY_CONVERSION_NO_NUMBER exception'.  
ENDTRY.  

12. Packed Number Handling

lv_pack =  '33.12456'.  
WRITE /: '12. Packed5_2 (33.12456): '. WRITE lv_pack.  

13. Exception: Packed Number with EXACT #()

TRY.  
    lv_pack = EXACT #( 1 / 8 ).  
    WRITE /: lv_pack.  
  CATCH cx_sy_conversion_rounding.  
    WRITE /: '13. Packed5_2 (1/8) w/ EXACT#: CX_SY_CONVERSION_ROUNDING exception'.  
ENDTRY.  

14. Date Handling

DATA lv_date1 TYPE sy-datum.  
lv_date1 =  '20241233'.  
WRITE /: '14. Date (20241233): '. WRITE lv_date1 USING EDIT MASK '__/__/____'.  

15. Exceptions: Date with EXACT #()

TRY.  
    lv_date = EXACT #( '202401233' ).  
    WRITE /: lv_date.  
  CATCH cx_sy_conversion_no_date.  
    WRITE /: '15. Date (20241233) w/ EXACT#: CX_SY_CONVERSION_NO_DATE exception'.  
ENDTRY.  

16. Date to Integer

lv_int =  sy-datum.  
WRITE /: '16. Date to Integer (sy-datum) = Days from 01.01.0001): '.  
WRITE: sy-datum USING EDIT MASK '__/__/____', ' --> ', lv_int.  

17. Integer to Date

lv_int = lv_int + 1.  
lv_date1 =  lv_int.  
WRITE /: '17. Integer to Date (sy-datum + 1) = Days from 01.01.0001): '.  
WRITE: lv_int, ' --> ', lv_date1 USING EDIT MASK '__/__/____'.  

18. Time to Integer

lv_int =  sy-uzeit.  
WRITE /: '18. Time to Integer (sy-uzeit) = Seconds from 00:00:00): '.  
WRITE: sy-uzeit USING EDIT MASK '__:__:__', ' --> ', lv_int.  

19. Integer to Time

DATA lv_time1 TYPE sy-uzeit.  
lv_int = lv_int + 3600.  
lv_time1 =  lv_int.  
WRITE /: '19. Integer to Time (sy-uzeit + 3600 sec) = Seconds from 00:00:00): '.  
WRITE: lv_int, ' --> ', lv_time1 USING EDIT MASK '__:__:__'.  

20. Direct Date Difference

lv_date = '20240101'.  
lv_int = sy-datum - lv_date.  
WRITE /: '20. Date Difference (sy-datum) - ''20240101''): '. WRITE lv_int.  

21. Character from Packed

lv_char = '1.23'.  
WRITE /: '21. Packed(1.23) to Character3: '. WRITE lv_char.  

 

UTC Long Handling

Converting various data types to UTC long often results in exceptions since these formats might not be compatible.

22. Alphanumeric String to UTClong

TRY.
    lv_utc lv_str.
    WRITE /'22. String to UTC long: 'WRITE lv_utc.
  CATCH cx_sy_conversion_no_date_time.
    WRITE /'22. String to UTC long: CX_SY_CONVERSION_NO_DATE_TIME exception'WRITE lv_utc.
ENDTRY.

23. Character3 to UTClong

lv_char lv_str.
TRY.
    lv_utc lv_char.
    WRITE /'23. Character3 to UTC long: 'WRITE lv_utc.
  CATCH cx_sy_conversion_no_date_time.
    WRITE /'23. Character3 to UTC long: CX_SY_CONVERSION_NO_DATE_TIME exception'WRITE lv_utc.
ENDTRY.

24. Numeric String to UTClong

TRY.
    lv_str '20241220'.
    lv_utc lv_str.
    WRITE /'24. String (20241220) to UTC long: 'WRITE lv_utc.
  CATCH cx_sy_conversion_no_date_time.
    WRITE /'24. String (20241220) to UTC long: CX_SY_CONVERSION_NO_DATE_TIME exception'WRITE lv_utc.
ENDTRY.

25-29. Impossible UTC conversions

WRITE /'25. Date can''t be converted to UTC long'.
WRITE /'26. Time can''t be converted to UTC long'.
WRITE /'27. Number can''t be converted to UTC long'.
WRITE /'28. Integer can''t be converted to UTC long'.
WRITE /'29. Packed can''t be converted to UTC long'.

30. System UTClong.

lv_utc = utclong_current( ).  
WRITE /: '30. System UTC time: '. WRITE lv_utc.  

Conclusion

Navigating through data type conversions in ABAP can be a complex task that requires careful handling of exceptions and understanding of underlying data structures. By exploring these 30 examples, you gain a clearer perspective on how conversions impact data integrity and application logic.

Feel free to experiment with these examples in your own systems and unlock new insights into the world of ABAP programming.

Program Output:

01. String: ABCD12.3
02. String to Character3: ABC
03. String to Character3 w/ EXACT#: CX_SY_CONVERSION_DATA_LOSS exception
04. String to integer: CX_SY_CONVERSION_NO_NUMBER exception
05. String to date: .312ABCD
06. String to date w/ EXACT#: CX_SY_CONVERSION_NO_DATE exception
07. String to time: ABCD12
08. String to time w/ EXACT#: CX_SY_CONVERSION_NO_TIME exception
09. String to number3: 123
10. String to number3 w/ EXACT#: CX_SY_CONVERSION_NO_NUMBER exception
11. String to Packed5_2: CX_SY_CONVERSION_NO_NUMBER exception
12. Packed5_2 (33.12456): 33,12
13. Packed5_2 (1/8) w/ EXACT#: CX_SY_CONVERSION_ROUNDING exception
14. Date (20241233): 33/12/2024
15. Date (20241233) w/ EXACT#: CX_SY_CONVERSION_NO_DATE exception
16. Date to Integer (sy-datum) = Days from 01.01.0001): 20/12/2024 > 739.241
17. Integer to Date (sy-datum + 1) = Days from 01.01.0001): 739.242 > 21/12/2024
18. Time to Integer (sy-uzeit) = Seconds from 00:00:00): 11:50:51 > 42.651
19. Integer to Time (sy-uzeit + 3600 sec) = Seconds from 00:00:00): 46.251 > 12:50:51
20. Date Difference (sy-datum) - '20240101'): 354
21. Packed(1.23) to Character3: 1.2
22. String to UTC long: CX_SY_CONVERSION_NO_DATE_TIME exception
23. String (20241220) to UTC long: CX_SY_CONVERSION_NO_DATE_TIME exception
24. Character3 to UTC long: CX_SY_CONVERSION_NO_DATE_TIME exception
25. Date can't be converted to UTC long
26. Time can't be converted to UTC long
27. Number can't be converted to UTC long
28. Integer can't be converted to UTC long
29. Packed can't be converted to UTC long
30. System UTC time: 20.12.2024 06:20:51,5742130

Happy Coding!