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.
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.
REPORT ztest_sk.
DATA: lv_int TYPE i,
lv_date TYPE d,
lv_time TYPE t,
lv_numc(3) TYPE n,
lv_str TYPE string,
lv_char TYPE c LENGTH 3,
lv_pack TYPE p LENGTH 5 DECIMALS 2,
lv_utc TYPE utclong.
lv_str = 'ABCD12.3'.
WRITE /: '01. String: '. WRITE lv_str.
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.
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.
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.
lv_date = lv_str. WRITE /: '05. String to date: '. WRITE lv_date.
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. lv_time = lv_str. WRITE /: '07. String to time: '. WRITE lv_time.
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. lv_numc = lv_str. WRITE /: '09. String to number3: '. WRITE lv_numc.
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. 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. lv_pack = '33.12456'. WRITE /: '12. Packed5_2 (33.12456): '. WRITE lv_pack.
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. DATA lv_date1 TYPE sy-datum. lv_date1 = '20241233'. WRITE /: '14. Date (20241233): '. WRITE lv_date1 USING EDIT MASK '__/__/____'.
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. lv_int = sy-datum. WRITE /: '16. Date to Integer (sy-datum) = Days from 01.01.0001): '. WRITE: sy-datum USING EDIT MASK '__/__/____', ' --> ', lv_int.
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 '__/__/____'.
lv_int = sy-uzeit. WRITE /: '18. Time to Integer (sy-uzeit) = Seconds from 00:00:00): '. WRITE: sy-uzeit USING EDIT MASK '__:__:__', ' --> ', lv_int.
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 '__:__:__'.
lv_date = '20240101'. lv_int = sy-datum - lv_date. WRITE /: '20. Date Difference (sy-datum) - ''20240101''): '. WRITE lv_int.
lv_char = '1.23'. WRITE /: '21. Packed(1.23) to Character3: '. WRITE lv_char.
Converting various data types to UTC long often results in exceptions since these formats might not be compatible.
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.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.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.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'.lv_utc = utclong_current( ). WRITE /: '30. System UTC time: '. WRITE lv_utc.
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.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 34 | |
| 20 | |
| 18 | |
| 16 | |
| 16 | |
| 15 | |
| 13 | |
| 12 | |
| 11 | |
| 10 |