SQL,在日期列中添加时间字段
Posted
技术标签:
【中文标题】SQL,在日期列中添加时间字段【英文标题】:SQL, Add a time field in a date column 【发布时间】:2018-04-24 15:00:23 【问题描述】:我有一个给我时间的领域,它是这样表达的:
上午 7:00
问题是当我尝试将它添加到应该包含时间的列时,它给了我错误,因为格式错误。
我尝试使用 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;
请注意,日期数据类型不能更改为其他类型。
感谢您的关注。
达尼洛
【问题讨论】:
您在单独的字段中存储时间是什么?您应该将两者放在一列中。 分配必须将它们实际放在单独的列中。我希望它是一样的,它会让我的生活更轻松。 无论如何,您都应该将它们放在一列中。您可以拆分日期和时间,例如按虚拟列或视图。 【参考方案1】:为什么将时间存储在单独的字段中?你知道DATE
有一个“时间”组件吗?分离字段几乎没有意义:
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
列出了所有列。
【讨论】:
我明白,在这种情况下,我想满足拥有两列的要求(在练习的要求中指定)我总是可以复制日期和时间(创建 2 列,其中一列只有日期另一个同时包含日期和时间),然后在使用 select 语句调用两列时,我只得到记录的一部分。我认为这可以做到。以上是关于SQL,在日期列中添加时间字段的主要内容,如果未能解决你的问题,请参考以下文章
SQL Server 2008 仅从字段中选择日期 [重复]