java.sql.SQLException: ORA-01843: not a valid month when calling a procedure

Darshan Lila picture Darshan Lila · Aug 29, 2014 · Viewed 7.5k times · Source

I have application which has a procedure as follows:

procedure get_full_data (list OUT sys_refcursor,id in number, 
                                   code in varchar2,type in varchar2) is
      year_in number;                                
begin
    year_in := to_number(to_char(to_date(sysdate,'DD-MON-RRRR'),'RRRR')); // line 628
        open list for
        select * from my_tab 
        where code_present = code AND to_number(substr(year,1,4)) in (year_in,year_in-1,year_in-2)
        and minth in(select max(month) from my_tab where code_present = code
                         and to_number(substr(year,1,4)) in (year_in,year_in-1,year_in-2)
                         group by substr(year,1,9))
        order by to_number(substr(year,1,4)) desc;
END get_full_data;

And Here's the Java code which calls this procedure (I'm using hibernate over here):

List<MyBean> salesApproval=session.getNamedQuery("get_data").setInteger("id",my_id).setString("code",my_code).setString("type",my_type).list();

MyBean is the class holds the data returned by procedure.

Problem:
When I host my application on windows system it works fine with desired result.
However when I host my application on Linux Ubuntu 12.04, it gives following error:

java.sql.SQLException: ORA-01843: not a valid month
ORA-06512: at "db.proc_pack", line 628

Line number 628 is mentioned in procedure.

Answer

Maheswaran Ravisankar picture Maheswaran Ravisankar · Aug 29, 2014
year_in := to_number(to_char(sysdate,'RRRR')); // line 628

SYSDATE is already returning date, so TO_DATE() conversion on it goes wrong..

To understand why the error is.. TO_DATE(SYSDATE,'...') will become TO_DATE('Date in Default Format','YOUR FORMAT'); So, more chances of it to go wrong..

Your default format will be NLS_DATE_FORMAT of your session! This depend on the platform and session parameters in different hosts!

Example:

SQL> SELECT value FROM   nls_database_parameters WHERE  parameter = 'NLS_DATE_FORMAT';

VALUE
--------------------------------------------------------------------------------
DD-MON-RR

SQL>  select to_date(sysdate,'MON-DD-RR') from dual;
 select to_date(sysdate,'MON-DD-RR') from dual
                *
ERROR at line 1:
ORA-01843: not a valid month

SQL> ALTER SESSION SET nls_date_format = 'MON-DD-RR';

Session altered.

SQL> select to_date(sysdate,'MON-DD-RR') from dual;

TO_DATE(S
---------
AUG-29-14

Oracle Document Reference