根据时间戳和时间间隔计算结束时间

时间:2018-11-05 03:38:40

标签: sql oracle

我是SQL新手。我一直被这个问题困扰。我已经能够完成其中的一些操作,但似乎无法正确输入信息以获取结束时间。

问题:使用匿名PL / SQL程序打印出“ Party City”事件组织者的电子邮件以及事件的开始时间和结束时间。使用隐式游标并处理异常。

这是我到目前为止的进展:

declare
o_email varchar(70);
s_tart_time timestamp;
begin
select oemail, start_time
into o_email, s_tart_time 
from  organizer o, event e
where e.oid = o.oid and
ename = 'Party City';
dbms_output.put_line ('email is: ' || o_email);
dbms_output.put_line ('start time is: ' || s_tart_time);
Exception
    when no_data_found then
    Dbms_output.put_line('No Data Found');
END;

表格:

create table event
(eid int, --- event id 
oid int, --- organizer id 
ename varchar(50), --- event name 
lid int, --- location id 
start_time timestamp, -- start time of event
duration interval day to second, --- duration of event, 
status int, --- 1 scheduled, 2 canceled, 3 finished 
primary key(eid), 
foreign key (lid) references location,
foreign key (oid) references organizer);

insert into event values (1, 1,'Party City', 1, timestamp '2018-9-6 10:00:00.00',interval '2' hour, 3);

谢谢!

1 个答案:

答案 0 :(得分:1)

Oracle SQL支持日期/时间算术,因此您只需在时间戳记中添加一个间隔即可获取结束时间。例如:

declare
o_email varchar(70);
s_tart_time timestamp;
end_time timestamp;
begin
select oemail, start_time, start_time + duration
into o_email, s_tart_time, end_time
from  organizer o, event e
where e.oid = o.oid and
ename = 'Party City';
dbms_output.put_line ('email is: ' || o_email);
dbms_output.put_line ('start time is: ' || s_tart_time);
dbms_output.put_line ('end time is: ' || end_time);
Exception
    when no_data_found then
    Dbms_output.put_line('No Data Found');
END;
/

email is: bla
start time is: 06-SEP-18 10.00.00.000000 AM
end time is: 06-SEP-18 12.00.00.000000 PM

LiveSQL-注意:由于您没有在问题中包含organizer表定义,因此我不得不注释掉一些位。