0

I don't have any experience in PL/SQL but I think I need it.

I have the following problem:

I have a table which contains:

Object  Station Time
744          2  23:40:00
744          3  23:45:00
744          4  23:48:00
744          8  23:59:00
744          9  00:02:00
744          13 00:15:00

Now I need a new field with date:

Object  Station Time    Date
744          2  23:40:00    26.03.2013
744          3  23:45:00    26.03.2013
744          4  23:48:00    26.03.2013
744          8  23:59:00    26.03.2013
744          9  00:02:00    27.03.2013
744         13  00:15:00    27.03.2013

You can see that’s the date has been changed if the time has reached 00:00:00

In SQL I don’t have any idea how I could do this - so now I think I need a PL/SQL script to create the Date.

4

2 回答 2

0

由于您既没有说明创建表的数据类型,也没有说明表可以包含哪些可能的值,也没有说明为什么要产生所需的结果,因此不可能提出正确的解决方案。但你可能会从类似的东西开始

create table tq84_t (
  station  number,
  time     varchar2(8)
);

insert into tq84_t values ( 2, '23:40:00');
insert into tq84_t values ( 3, '23:45:00');
insert into tq84_t values ( 4, '23:48:00');
insert into tq84_t values ( 8, '23:59:00');
insert into tq84_t values ( 9, '00:02:00');
insert into tq84_t values (13, '00:15:00');

select 
  station,
  time,
  to_char(trunc(sysdate) -1 +
         sum(next_day) over (order by station), 'dd.mm.yyyy') date_
from (
  select
     case when nvl(lag(time) over (order by station), chr(0)) < time 
          then 0 
          else 1 end next_day,
    time,
    station
  from
    tq84_t
)
order by 
  station;
于 2013-03-28T07:34:58.967 回答
-2
INSERT INTO #your_table
SELECT 744 as OBJECT, 13 as Station , '00:15:00' as your time , CONVERT(varchar ,  GETDATE(), 103)

^^ 这仅在您插入当前日期值时才有效;

或者您可以简单地使用 DATETIME 字段

CREATE TABLE #yortable 
(OBJECT INT, Station int ,  Arrival_date datetime )

例如,您有汽车站,它应该在 '2013/03/28 23:30:00' 到达,并且它已经晚了。您只需为其增加价值

update #yortable 
set Arrival_date  = DATEADD(mi, 31, Arrival_date  ) 
WHERE Station=1 and object=744

mi- 分 hh - 小时 ss - 秒

结果

'2013/03/29 00:01:00'

于 2013-03-28T07:20:38.070 回答