“ORA-01843: 不是一个有效的月份”错误

Posted

技术标签:

【中文标题】“ORA-01843: 不是一个有效的月份”错误【英文标题】:"ORA-01843: not a valid month" error 【发布时间】:2018-04-19 07:42:50 【问题描述】:

我在尝试从代码运行程序时遇到此错误。

“ORA-01843:不是有效月份 ORA-06512:在“NOSTAS_OWNER.INSERT_EXPORTMATRIX”,第 68 行 ORA-06512:在第 1 行”

我将日期作为“DD/MM/YYYY”字符串传递。不确定是什么导致了错误。在 SQL Developer 中运行它时,它给出了这个错误

ORA-06502:PL/SQL:数字或值错误:字符到数字的转换错误 ORA-06512:在“NOSTAS_OWNER.INSERT_EXPORTMATRIX”,第 68 行 ORA-06512:在第 17 行

create or replace PROCEDURE INSERT_EXPORTMATRIX
(
  keyClass export_matrix.EXM_KEY_CLASS%type,
  keyClassValue export_matrix.EXM_KEYCLASS_VALUE%type,
  changedClass export_matrix.EXM_CHANGED_CLASS%type,
  changedClassValue export_matrix.EXM_CHANGEDCLASS_VALUE%type,
  dtTo IN VARCHAR2,
  dtFrom IN VARCHAR2,
--    dtTo export_matrix.EXM_DATEFROM%type,
--    dtFrom EXPORT_MATRIX.EXM_DATETO%type,
  vresult OUT NUMBER
) AS 

iskeyClass number;
--today varchar2(50);
--today date;
fvvECCN number;
fvvALNR number;

BEGIN
iskeyClass := 0;
fvvECCN := 0;
fvvALNR := 0;

SAVEPOINT start_tran;

  SELECT count(*) into iskeyClass FROM EXPORT_MATRIX 
    WHERE EXM_KEY_CLASS=keyClass and EXM_KEYCLASS_VALUE=keyClassValue; 
--    SELECT to_date(SYSDATE,'DD/MM/YYYY') into today  FROM DUAL;   
    SELECT count(*) into fvvECCN FROM FIELDS_VALUE_VALIDATION WHERE
        --FVV_FAVL_NAME = 'ECCN' and FVV_VALUE = keyClassValue and (dtTo > trunc(sysdate));
        FVV_FAVL_NAME = 'ECCN' and FVV_VALUE = keyClassValue and (sysdate between dtFrom and dtTo) or dtTo is null;

    SELECT count(*) into fvvALNR FROM FIELDS_VALUE_VALIDATION WHERE
        FVV_FAVL_NAME = 'ALNR' and FVV_VALUE = changedClassValue and (sysdate between dtFrom and dtTo) or dtTo is null;

BEGIN
-- if (iskeyClass = 1 and (dtTo < trunc(sysdate))) or iskeyClass = 0  then
    if (iskeyClass = 1 and (dtTo between DtFrom and sysdate)) or iskeyClass = 0 then
       Insert into EXPORT_MATRIX (EXM_KEY_CLASS, EXM_KEYCLASS_VALUE, EXM_CHANGED_CLASS, 
        EXM_CHANGEDCLASS_VALUE, EXM_DATEFROM) 
        values (keyClass,keyClassValue,changedClass,changedClassValue,trunc(sysdate));

        if fvvECCN = 0 then
                   Insert into FIELDS_VALUE_VALIDATION (FVV_FAVL_NAME,FVV_VALUE,FVV_DATEFROM) 
                    values ('ECCN',keyClassValue,trunc(sysdate));
        end if;
                if fvvALNR = 0 then
                   Insert into FIELDS_VALUE_VALIDATION (FVV_FAVL_NAME,FVV_VALUE,FVV_DATEFROM) 
                    values ('ALNR',changedClassValue,trunc(sysdate));
                end if;
  end if;

--  if keyClass = 1 and (dtTo > trunc(sysdate)) then
    if keyClass = 1 and (sysdate between dtFrom and dtTo) or dtTo is null then
         update EXPORT_MATRIX set    
          EXM_DATETO=to_date(dtTo)        
       WHERE EXM_KEY_CLASS=keyClass and EXM_KEYCLASS_VALUE=keyClassValue;  
  end if;

  END;
  commit;
  vresult := 1;
  EXCEPTION
WHEN OTHERS THEN

  ROLLBACK TO start_tran;
  RAISE;
END INSERT_EXPORTMATRIX;

【问题讨论】:

【参考方案1】:

将字符串转换为日期时应添加掩码:

to_date(dtTo,'DD/MM/YYYY') 

此外,在与 sysdate 进行比较时,您还应该将字符串转换为日期,因此:

sysdate between to_date(dtFrom,'DD/MM/YYYY') and to_date(dtTo,'DD/MM/YYYY') 

【讨论】:

以上是关于“ORA-01843: 不是一个有效的月份”错误的主要内容,如果未能解决你的问题,请参考以下文章

ORACLE - ORA-01843: 不是一个有效的月份

cx_Oracle.DatabaseError: ORA-01843: 不是一个有效的月份

从存储过程刷新物化视图时出错(不是有效月份)

Chromecast 网络 I/O 错误

boost::asio 错误“I/O 操作已中止...”

数据帧考拉到增量表:错误:调用 o237.save 时发生错误