1、字符型到日期型
标准做法一般调用函数:DATE_CHECK_PLAUSIBILITY,如果不需要太详细的错误信息,根据SAP类型转换规则,也可以写出如下代码:
DATA: dd TYPE sy-datum,
cc(8).
cc(8).
cc = '20110728'.
dd = cc.
IF dd = 0.
WRITE: dd,'错误'.
ELSE.
WRITE: dd,'正确'.
ENDIF.
2、字符型到时间类型
函数:TIME_CHECK_PLAUSIBILITY,或者如第一条写代码
3、字符型到数值型
DATA: pp TYPE p DECIMALS 2.
DATA: cxroot TYPE REF TO cx_root,
exception_msg(1000),
msg(1000).
PARAMETERS: str(6) .
**方法1
TRY .
pp = str.
CATCH cx_root INTO cxroot.
msg = cxroot->get_text( ).
WRITE:/ msg.
ENDTRY.
**方法2
CATCH SYSTEM-EXCEPTIONS convt_no_number = 1.
pp = str .
ENDCATCH.
IF sy-subrc <> 0.
WRITE / 'Error!'.
ENDIF.
**方法3
DATA str_tmp TYPE string .
str_tmp = str.
IF NOT str_tmp CO '0123456789.-'.
WRITE / 'Err'.
ELSE.
pp = str_tmp.
ENDIF.
WRITE / pp.
**方法1和方法2不可以在同一个程序使用
DATA: cxroot TYPE REF TO cx_root,
exception_msg(1000),
msg(1000).
PARAMETERS: str(6) .
**方法1
TRY .
pp = str.
CATCH cx_root INTO cxroot.
msg = cxroot->get_text( ).
WRITE:/ msg.
ENDTRY.
**方法2
CATCH SYSTEM-EXCEPTIONS convt_no_number = 1.
pp = str .
ENDCATCH.
IF sy-subrc <> 0.
WRITE / 'Error!'.
ENDIF.
**方法3
DATA str_tmp TYPE string .
str_tmp = str.
IF NOT str_tmp CO '0123456789.-'.
WRITE / 'Err'.
ELSE.
pp = str_tmp.
ENDIF.
WRITE / pp.
**方法1和方法2不可以在同一个程序使用