Hi Joao,
I think the problem is because DOCDATE is not stored as date in Oracle database. Please note that SAP stores date as character and the format is YYYYMMDD.
So the solution is to change DOCDATE to date and then you can get the month or year portion of it.
TO_CHAR(TO_DATE(begda, 'YYYYMMDD'), 'YYYY')
To give you an example, please find it following.
INFOTYPES: 0105.
DATA: p_con_ref TYPE REF TO cl_sql_connection.
DATA: l_stmt TYPE string,
l_stmt_ref TYPE REF TO cl_sql_statement,
l_dref TYPE REF TO data,
l_sqlerr_ref TYPE REF TO cx_sql_exception,
l_res_ref TYPE REF TO cl_sql_result_set,
lv_param1 TYPE c LENGTH 30,
lv_param2 TYPE c LENGTH 10,
lt_result TYPE STANDARD TABLE OF pa0105,
lwa_result LIKE LINE OF lt_result.
TRY.
CREATE OBJECT p_con_ref.
CONCATENATE 'select * from pa0105 '
' where usrid_long = ? '
' AND usrty = ''0010'' '
' AND TO_CHAR(TO_DATE(begda, ''YYYYMMDD''), ''YYYY'') = ? ' "Please note you need to put two single apostrophe instead of double apostrophe
INTO l_stmt SEPARATED BY space.
l_stmt_ref = p_con_ref->create_statement( ).
lv_param1 = 'YOUREMAIL@YOURCOMPANY.COM.AU'.
GET REFERENCE OF lv_param1 INTO l_dref.
l_stmt_ref->set_param( l_dref ).
lv_param2 = '2009'.
GET REFERENCE OF lv_param2 INTO l_dref.
l_stmt_ref->set_param( l_dref ).
l_res_ref = l_stmt_ref->execute_query( l_stmt ).
GET REFERENCE OF lt_result into l_dref.
l_res_ref->set_param_table( l_dref ).
IF l_res_ref->next_package( ) > 0 .
LOOP AT lt_result INTO lwa_result.
WRITE:/ lwa_result-usrid_long, lwa_result-begda.
ENDLOOP.
ENDIF.
CATCH cx_sql_exception INTO l_sqlerr_ref.
ENDTRY.
Hopefully this will resolve your issue.
Additionally, you can use SQL command editor in DB02 transaction to test if your native sql can be executed properly or not.
Regards,
Stevanic