【问题标题】:ORA-01843: not a valid month when inserting a dateORA-01843: 插入日期时月份无效
【发布时间】:2021-11-06 06:58:05
【问题描述】:

我正在使用 Apex Oracle 运行将数据/表与现有架构合并的脚本文件。这是完整的脚本文件。 每个插入命令都会产生错误,声明 not a valid month。 Alter 命令产生一个错误,声明Column Type incompatible with referenced column type

脚本文件:

--A1_rr_upd.txt file 

--dropping the table if already exists
DROP TABLE RRSTAFF;

--creating new table for RRSTAFF

CREATE TABLE RRSTAFF (
    staff_num CHAR(4) PRIMARY KEY,
    name VARCHAR(20),
    gender CHAR(1),
    date_join DATE,
    date_resign DATE,
    contact_num NUMBER(11),
    address VARCHAR(255)
);

--Adding new hired staff
INSERT INTO RRSTAFF VALUES ('s001','Adrian','M','2021/07/02',null,'60122000000','6 Jalan BU6, Petaling Jaya,Selangor');
INSERT INTO RRSTAFF VALUES ('s002','Jewel','F','2021/07/12',null,'60123000000','2 Jalan PJS2, Sunway,Selangor');
INSERT INTO RRSTAFF VALUES ('s003','Sean','M','2021/07/12',null,'60166000000','100 Sunway South K,Selangor');

--Adding new customer details
INSERT INTO rrcustomer VALUES ('1011','Dr','Brendan');
INSERT INTO rrcustomer VALUES ('1012','Dr','Haya');

--Adding new records into models
INSERT INTO model VALUES ('LGC83','LG C1 83 in OLED 4K TV', '500');
INSERT INTO model VALUES ('LGG77','LG Gallery 77 in OLED 4K TV', '400');
INSERT INTO model VALUES ('SNY43','Sony 43 in X75 4K Ultra HD Android TV', '200');
INSERT INTO model VALUES ('SHA50','Sharp 50 in  Full HD Basic TV ', '80');

--Adding new records into appliance
INSERT INTO appliance VALUES ('2010','LGC83','E',null);
INSERT INTO appliance VALUES ('2011','LGC83','E',null);

--Altering the HIRE table to link it with RRSTAFF using staff_id as Foriegn Key
ALTER TABLE
    hire ADD(
        staff_id VARCHAR(4),
            FOREIGN KEY (staff_id) REFERENCES rrstaff(staff_num)
    );

--Adding new hire records
INSERT INTO hire VALUES ('2010','2021/08/02','1011','2021/08/08','s001');
INSERT INTO hire VALUES ('2010','2021/08/22','1012','2021/08/28','s001');
INSERT INTO hire VALUES ('2011','2021/08/12','1013',null,'s001');

【问题讨论】:

  • TO_DATE('2021/08/02', 'YYYY/MM/DD') 会起作用
  • 您正试图将字符串文字插入到 DATE 类型的列中,这是一个内部二进制结构。因此,您依赖于隐含的 TO_DATE,并且控制 NLS_DATE_FORMAT 与您的字符串文字不匹配。这就是 为什么 其他人建议明确使用 TO_DATE。 FWIW,你为什么要为一堆不涉及的表显示插入,并且你没有显示表创建 DDL?

标签: sql oracle oracle-apex


【解决方案1】:

在 Oracle 中,您使用 date 关键字和 ISO 标准 YYYY-MM-DD 格式的字符串来表示 date 常量。例如:

INSERT INTO RRSTAFF
    VALUES ('s001', 'Adrian', 'M', DATE '2021-07-02', null, '60122000000', '6 Jalan BU6, Petaling Jaya,Selangor');

Here 是一个 dbfiddle。

【讨论】:

  • 错误:ORA-01861: literal does not match format string 正在生成
  • @MuhammadAbbas 。 . .它工作正常。不过,您必须修复所有日期。
【解决方案2】:

使用TO_DATE('2021/07/02', 'YYYY/MM/DD') 将您的日期值转换为标准数据库日期格式

将上述日期值替换为您的日期列

【讨论】:

    【解决方案3】:

    使用 to_date 使脚本健壮且独立于隐式格式。它还使代码更具可读性,因为 to_date 在其语法中非常明显。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-03
      • 1970-01-01
      • 1970-01-01
      • 2014-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多