如何生成给定时间的日期?

时间:2009-11-03 06:13:09

标签: sql sql-server vb.net sql-server-2005

使用VB.NET和SQL Server 2005

我想根据给定的Outtime生成日期,

输入的值如

ID  Date       Intime   Outtime  Holiday

001 23-02-2009 08:00:00 17:00:00 no
001 24-02-2009 17:00:00 08:00:00 no
001 25-02-2009 10:00:00 16:00:00 no
001 26-02-2009 18:00:00 20:00:00 no
001 27-02-2009                   yes

预期产出

表1

ID  Date       Intime   Outtime  DateIn     Dateout

001 23-02-2009 08:00:00 17:00:00 23-02-2009 23-02-2009
001 24-02-2009 17:00:00 08:00:00 24-02-2009 25-02-2009
001 25-02-2009 10:00:00 16:00:00 25-02-2009 25-02-2009
001 26-02-2009 18:00:00 20:00:00 26-02-2009 27-02-2009
001 27-02-2009     -        -        -         -

…,

从上表中,DateIn列值应显示相同的日期列值,但Dateout列应根据Outtime显示。

In a first row – Intime is 08:00:00 and Outtime is 17:00:00, So Dateout should display the same date
In a second row – Intime is 17:00:00 and Outtime is 08:00:00, So DateOut should display the next day date.
In a third row – Intime is 10:00:00 and Outtime is 16:00:00, so Dateout should display the same date
In a fourth row – Intime is 18:00:00 and Outtime is 21:00:00, so Dateout should display the next day date.
In a fifth row – Holiday column value is yes, so it should not display any value in DateIn and DateOut

Dateout列应比较Intime和Outtime值,如果Outtime小于Intime,则Dateout列应显示第二天的日期。

如果Holiday = Yes,则它应显示为空白列

对于上述情况,如何进行SQL查询?

是否有可能在vb.net中,哪个更快的sql查询或vb.net代码?

需要SQL Query或VB.NET代码才能满足上述条件。

3 个答案:

答案 0 :(得分:1)

我会建议:

SELECT t.id,
       t.date_column,
       t.intime,
       t.outtime,
       t.date AS datein,
       CASE
         WHEN t.outime < t.intime THEN
           DATEADD(dd, 1, t.date)
         ELSE
           t.date
       END AS dateout
  FROM TABLE t
 WHERE t.holiday = 'no'
UNION ALL
SELECT h.id,
       h.date_column,
       NULL,
       NULL,
       NULL,
       NULL
  FROM TABLE h
 WHERE h.holiday = 'yes'
ORDER BY date_column

...但是没有任何方法可以知道停播时间是否真的跨越到第二天(或更多)。 IE:1800的播出时间和2100的播出时间可能意味着第二天。

答案 1 :(得分:1)

这似乎有效(注意我的日期是美国格式,因为我就是这样:-)):

create table times
   (
   Id             integer,
   stDate         datetime,
   InTime         datetime,
   OutTime        datetime,
   Holiday        varchar(3)
   )
go

insert into times values (001, '02-23-2009', '08:00:00', '17:00:00', 'no')
insert into times values (001, '02-24-2009', '17:00:00', '08:00:00', 'no')
insert into times values (001, '02-25-2009', '10:00:00', '16:00:00', 'no')
insert into times values (001, '02-26-2009', '21:00:00', '20:00:00', 'no')
insert into times values (001, '02-27-2009', null, null, 'yes')
go

select * from times
go

select
   t.Id,
   stDate,
   InTime,
   OutTime,
   case
      when Holiday = 'no' then stDate
      else null
   end       DateIn,
   case
      when InTime > OutTime and Holiday = 'no' then stDate + 1
      when InTime < OutTime and Holiday = 'no' then stDate
      else null
   end       DateOut
from
   times t

结果:

Id         stDate                  InTime                  OutTime                 DateIn                  DateOut                 
---------- ----------------------- ----------------------- ----------------------- ----------------------- ----------------------- 
1          2009-02-23 00:00:00.000 1900-01-01 08:00:00.000 1900-01-01 17:00:00.000 2009-02-23 00:00:00.000 2009-02-23 00:00:00.000 
1          2009-02-24 00:00:00.000 1900-01-01 17:00:00.000 1900-01-01 08:00:00.000 2009-02-24 00:00:00.000 2009-02-25 00:00:00.000 
1          2009-02-25 00:00:00.000 1900-01-01 10:00:00.000 1900-01-01 16:00:00.000 2009-02-25 00:00:00.000 2009-02-25 00:00:00.000 
1          2009-02-26 00:00:00.000 1900-01-01 21:00:00.000 1900-01-01 20:00:00.000 2009-02-26 00:00:00.000 2009-02-27 00:00:00.000 
1          2009-02-27 00:00:00.000 NULL                    NULL                    NULL                    NULL                    

5 Row(s) affected

答案 2 :(得分:0)

您可以使用CASE语句添加OutTime小于InTime的日期,或者在假期时显示空白日期:

select case 
    when holiday = 'yes' then ''
    when OutTime < InTime then DateAdd(day,1,[Date])
    else [Date]
    end as DateOut