oracle日期转换错误

时间:2013-12-27 13:56:40

标签: sql oracle stored-procedures

当iv_from_date的输入以此格式04/01 / 2013给出时,我收到错误,当输入采用此格式时,它会完美地运行04 / Jan / 2013.nls日期格式为DD-MON_RR.My输入应该采用这种格式04/01 / 2013.How我这样做

CREATE OR REPLACE procedure Sp_SaveNewTaxPercentage(iv_category_id    number,
                                                    iv_tax_percentage number,
                                                    iv_from_date      varchar2,
                                                    iv_to_date        varchar2 default '12/31/9998',
                                                    iv_created_date   date,
                                                    ov_err_code       out nocopy varchar2,
                                                    ov_err_msg        out nocopy varchar2) is
  lv_category_id    varchar2(25);
  LV_TO_DATE        varchar2(25);
  lv_cat_id         varchar2(12);
  IV_TAX_ID         VARCHAR2(12);
  lv_tax_percentage varchar(20);
begin
  ov_err_code := 0;

  SELECT MAX(TAX_ID)
    INTO IV_TAX_ID
    FROM TAX_P
   WHERE CATEGORY_ID IN (iv_category_id);

  for J in (SELECT CATEGORY_ID, FROM_DATE, tax_percentage
              FROM TAX_P
             WHERE TAX_ID = IV_TAX_ID) loop

    SELECT to_date(iv_from_date, 'dd/mm/yyyy') - 1
      INTO LV_TO_DATE
      FROM DUAL;

    lv_cat_id         := J.CATEGORY_ID;
    lv_tax_percentage := j.tax_percentage;

    begin
      UPDATE TAX_P
         SET TO_dATE = LV_TO_DATE
       WHERE TAX_PERCENTAGE = lv_tax_percentage
         and to_date = iv_to_date;

      commit;

      insert into tax_p
        (TAX_ID,
         CATEGORY_ID,
         TAX_PERCENTAGE,
         FROM_DATE,
         TO_DATE,
         CREATE_DATE)
      values
        (tax_seq.nextval,
         iv_category_id,
         to_char(iv_tax_percentage, '99D99'),
         iv_from_date,
         iv_to_Date,
         sysdate);

      commit;

    end;

  end loop;

  IF LV_CAT_ID IS NULL THEN

    insert into tax_p
      (TAX_ID,
       CATEGORY_ID,
       TAX_PERCENTAGE,
       FROM_DATE,
       TO_DATE,
       CREATE_DATE)
    values
      (tax_seq.nextval,
       iv_category_id,
       to_char(iv_tax_percentage, '99D99'),
       iv_from_date,
       IV_TO_DATE,
       sysdate);

  END IF;

  commit;

  select 'Successfully Saved' into ov_err_msg from dual;

  commit;
Exception
  when others then
    rollback;
    ov_err_code := 1;
    ov_err_msg  := 'Error while saving' || SQLERRM;
end Sp_SaveNewTaxPercentage;

1 个答案:

答案 0 :(得分:1)

请避免使用VARCHAR2类型的变量和参数,实际上类型为DATE。 Oracle尝试隐式地将varchar参数转换为date。当格式与NLS_DATE_FORMAT中设置的格式相同时,它会失败。同时将iv_from_dateiv_to_date输入为DATE。如果表TAX_P中的相应列被输入为varchar2,则在insert语句中进行显式格式转换。