SQL,在日期列中添加时间字段

时间:2017-11-11 14:05:14

标签: sql oracle date oracle11g sqldatatypes

我有一个给我时间的字段,它表达如下:

早上7点

问题是,当我尝试将其添加到应该包含时间的列时,它会给我错误,因为格式错误。

我尝试使用to_char更改格式,但它给出了一个错误,说明该函数未实现。

这是代码。

CREATE TABLE call_info
(
    call_id NUMBER(4) 
    CONSTRAINT cio_call_id_CK CHECK (call_id >= 1000 AND call_id <= 9999),
    report_first VARCHAR2(10),
    report_last VARCHAR2(11) CONSTRAINT cio_report_last_NN NOT NULL,
    report_date DATE CONSTRAINT cio_report_date_NN NOT NULL,
    report_time DATE CONSTRAINT cio_report_time_NN NOT NULL,
    problem_code NUMBER(1) CONSTRAINT cio_problem_code_NN NOT NULL,
    service_code VARCHAR2(4),
    CONSTRAINT cio_call_id_PK PRIMARY KEY (call_id),
    CONSTRAINT cio_problem_code_FK FOREIGN KEY (problem_code) 
    REFERENCES problems(code),
    CONSTRAINT cio_service_code_FK FOREIGN KEY (service_code)
    REFERENCES services(code)   
)
;

BEGIN 
INSERT INTO call_info
VALUES ('1102','Bill','Madden',TO_DATE('29-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('7:00 PM','hh:mi AM'),'hh:mi-AM'),'2','PSEG');
INSERT INTO call_info
VALUES ('1103','Anita','Verno',TO_DATE('29-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('7:01 PM','hh:mi AM'),'hh:mi-AM'),'2','PSEG');
INSERT INTO call_info
VALUES ('1200','Emily','Vandalovsky',TO_DATE('29-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('7:45 PM','hh:mi AM'),'hh:mi-AM'),'2','PSEG');
INSERT INTO call_info
VALUES ('1111','Gary','Correa',TO_DATE('29-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('8:10 PM','hh:mi AM'),'hh:mi-AM'),'1','DPR');
INSERT INTO call_info
VALUES ('1101','Mickey','Mouse',TO_DATE('29-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('11:00 PM','hh:mi AM'),'hh:mi-AM'),'6','PSEG');
INSERT INTO call_info
VALUES ('1012','Minnie','Mouse',TO_DATE('29-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('11:21 PM','hh:mi AM'),'hh:mi-AM'),'1','DPR');
INSERT INTO call_info
VALUES ('1013','Goofy','Disney',TO_DATE('29-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('11:47 PM','hh:mi AM'),'hh:mi-AM'),'5','OEM');
INSERT INTO call_info
VALUES ('1040','Donald','Duck',TO_DATE('30-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('2:34 AM','hh:mi AM'),'hh:mi-AM'),'4','OEM');
INSERT INTO call_info
VALUES ('1501','Cinderella','Disney',TO_DATE('30-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('3:15 AM','hh:mi AM'),'hh:mi-AM'),'3','CSH');
INSERT INTO call_info
VALUES ('1506','Ernie','Sesame',TO_DATE('30-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('3:16 AM','hh:mi AM'),'hh:mi-AM'),'3','CSH');
INSERT INTO call_info
VALUES ('1007','Burt','Sesame',TO_DATE('30-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('3:18 AM','hh:mi AM'),'hh:mi-AM'),'3','CSH');
INSERT INTO call_info
VALUES ('1081','Bruce','Springsteen',TO_DATE('30-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('4:10 AM','hh:mi AM'),'hh:mi-AM'),'2','PSEG');
INSERT INTO call_info
VALUES ('1910','Chris','Christie',TO_DATE('30-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('4:25 AM','hh:mi AM'),'hh:mi-AM'),'7','OEM');
INSERT INTO call_info
VALUES ('1010','Mitt','Romney',TO_DATE('30-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('5:15 AM','hh:mi AM'),'hh:mi-AM'),'1','DPR');
INSERT INTO call_info
VALUES ('1015','Barack','Obama',TO_DATE('30-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('5:17 AM','hh:mi AM'),'hh:mi-AM'),'1','DPR');
INSERT INTO call_info
VALUES ('1019','Bruce','Wayne',TO_DATE('30-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('7:57 AM','hh:mi AM'),'hh:mi-AM'),'5','OEM');
INSERT INTO call_info
VALUES ('1314','Minas','Kousoulis',TO_DATE('30-OCT-17','DD-MON-YY'),TO_CHAR(TO_DATE('8:01 AM','hh:mi AM'),'hh:mi-AM'),'4','WTR');
END;

请注意,日期数据类型不能更改为其他数据类型。

感谢您的关注。

Danilo的

1 个答案:

答案 0 :(得分:0)

为什么要将时间存储在单独的字段中?您是否知道DATE有&#34;时间&#34;零件?分离字段几乎没有意义:

CREATE TABLE call_info (
    call_id NUMBER(4) 
    CONSTRAINT cio_call_id_CK CHECK (call_id >= 1000 AND call_id <= 9999),
    report_first VARCHAR2(10),
    report_last VARCHAR2(11) CONSTRAINT cio_report_last_NN NOT NULL,
    report_datetime DATE CONSTRAINT cio_report_date_NN NOT NULL,
    problem_code NUMBER(1) CONSTRAINT cio_problem_code_NN NOT NULL,
    service_code VARCHAR2(4),
    CONSTRAINT cio_call_id_PK PRIMARY KEY (call_id),
    CONSTRAINT cio_problem_code_FK FOREIGN KEY (problem_code) 
    REFERENCES problems(code),
    CONSTRAINT cio_service_code_FK FOREIGN KEY (service_code)
    REFERENCES services(code)   
);

然后我建议使用Oracles TIMESTAMP关键字插入值:

INSERT INTO call_info (call_id, report_first, report_last, report_datetime, problem_code, service_code)
    VALUES (1102, 'Bill', 'Madden', TIMETAMP '2017-10-29 19:00:00', 
2, 'PSEG');

请注意,我还删除了数字周围的单引号,insert列出了所有列。