[英]How to add minutes to a date(timestamp) in Oracle?
I created the following tables: 我创建了以下表格:
create table travel
(
code VARCHAR2(3),
date timestamp,
constraint pk_code_travel primary key(code)
);
create table extras
(
code_extra VARCHAR2(3),
code_travel VARCHAR2(3),
minutes NUMBER(3),
constraint pk_code_extra primary key(code_extra)
);
I inserted the following dates: 我插入了以下日期:
insert into travel (code, date)
values('T01',TO_TIMESTAMP('10/01/2016 15:30','DD/MM/YYYY HH24:MI'));
insert into travel (code, date)
values('T02',TO_TIMESTAMP('15/02/2016 17:45','DD/MM/YYYY HH24:MI'));
insert into travel (code, date)
values('T03',TO_TIMESTAMP('01/04/2016 22:00','DD/MM/YYYY HH24:MI'));
insert into extras (code_extra, code_travel, minutes)
values('E01', 'T01', 50);
insert into extras (code_extra, minutes)
values('E02', 'T02', 123);
insert into extras (code_extra, minutes)
values('E03', 'T03', 48);
The question is: how to add the minutes from the table "extras" to the date of "travel" table ? 问题是:如何将表“ extras”中的分钟数添加到“ travel”表中的日期?
For example the table "travel" should be: 例如,表“ travel”应为:
T01', 10/01/2016 15:30 + 50 minutes.
T01',2016年10月1日15:30 + 50分钟。
T02', 15/02/2016 17:45 + 123 'minutes.
T02',15/02/2016 17:45 + 123'分钟。
T03', 01/04/2016 22:00 + 48 minutes.
T03',01/04/2016 22:00 + 48分钟。
All help will be appreciated. 所有帮助将不胜感激。
You can create a table like this: 您可以创建一个像这样的表:
create table extras
(
code_extra VARCHAR2(3),
code_travel VARCHAR2(3),
minutes INTERVAL DAY TO SECOND(0),
constraint pk_code_extra primary key(code_extra)
);
Then you can insert interval values by one of the following methods: 然后,您可以通过以下方法之一插入间隔值:
INSERT INTO extras (code_extra, code_travel, minutes)
VALUES('E01', 'T01', INTERVAL '50' MINUTE);
INSERT INTO extras (code_extra, minutes)
VALUES('E02', 'T02', 123 * INTERVAL '1' MINUTE);
INSERT INTO extras (code_extra, minutes)
VALUES('E03', 'T03', NUMTODSINTERVAL(48, 'MINUTE'));
Or as a select statement from your existing tables: 或作为现有表的选择语句:
SELECT code, "DATE" + minutes * INTERVAL '1' MINUTE
FROM travel
JOIN extras ON code = code_extra;
or 要么
SELECT code, "DATE" + NUMTODSINTERVAL(minutes, MINUTE)
FROM travel
JOIN extras ON code = code_extra;
btw, you should not use reserved words like DATE
as column name (that's the reason why I enclosed it by double-quotes) 顺便说一句,您不应使用
DATE
类的保留字作为列名(这就是我用双引号将其括起来的原因)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.