2 Replies Latest reply: Aug 31, 2012 9:28 AM by 921443 RSS

    ORA-01841

    921443
      Hi expert,

      I have two statement in my procedure :
      r_period IN VARCHAR2
      v_from_date date;
      v_to_date date;

      v_from_date := to_date('1/4/'||to_char(r_period),'dd/mm/yyyy');

      v_to_date := to_date('31/3/'||to_char((to_number(r_period)-5)),'dd/mm/yyyy');

      when I ran procedure , got following error message:

      "ORA-01841 :(full) year must be between -4713 and +9999, and not be 0 "

      appreciate very much if help me to resolve it.

      Many Thanks,
        • 1. Re: ORA-01841
          Frank Kulash
          Hi,

          I can't re-produce the problem.

          Whenever you have a problem, please post a complete test script that people can run to re-create the problem and test their ideas. In this case, include the complete procedure code (or a simplified version that has the same problem) and the code that you use to call it.
          Always say which version of Oracle you're using (for example, 11.2.0.2.0).
          See the forum FAQ {message:id=9360002}
          918440 wrote:
          Hi expert,

          I have two statement in my procedure :
          r_period IN VARCHAR2
          v_from_date date;
          v_to_date date;

          v_from_date := to_date('1/4/'||to_char(r_period),'dd/mm/yyyy');

          v_to_date := to_date('31/3/'||to_char((to_number(r_period)-5)),'dd/mm/yyyy');
          Usuaully, when people talk about dates "from x to y", x is earlier than y. You're setting v_from_date to be over 5 years later than v_to_date. That's not the cause of the ORA-01841 error, or any other Oracle error, but it is likely to confuse whoever has to maintain this code.
          when I ran procedure , got following error message:

          "ORA-01841 :(full) year must be between -4713 and +9999, and not be 0 "
          That's one of those error messages that actually means what it says. Apparantly, you're calling the procedure with a bad value for r_period. I can't say anthing more specific until you post a complete script.

          Converting from NUMBERs to VARCHAR2s to DATEs is very error prone. Try to avoid doing so many conversions. For example, you're doing 3 data type conversions to compute v_from_date. You could get the same results with only 1 conversion, like this:
          v_to_date   := ADD_MONTHS ( TO_DATE ( '31/3/' || r_period
                                                    , 'DD/MM/YYYY'
                                  )
                           , -5 * 12
                              );
          Personally, I would do it this way:
          v_to_date   := ADD_MONTHS ( TRUNC ( TO_DATE (r_period, 'YYYY')
                                , 'YEAR'
                                ) - 1
                           , -57
                              );
          which also uses only 1 conversion, and that conversion is very simple. You can still get errors, including ORA-01841, if you pass a bad value for r_period, but the errors will be easier to find and fix.
          • 2. Re: ORA-01841
            921443
            Frank, appreciate very much. I resolved this issue by your suggestion