【问题标题】:How to generate a date for the given time?如何生成给定时间的日期?
【发布时间】:2009-11-03 06:13:09
【问题描述】:

使用 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 查询或 VB.NET 代码。

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

    • 假设 Intime 是 18:00:00,Outtime 是 21:00:00。这意味着Outtime是第二天,所以如何查询这个条件。
    • 该条件不符合您的任何其他条件。你知道,如果你只是想找到一个 8 小时轮班的结束日期和时间,你肯定会很难做到。如果您知道轮班的开始时间(开始日期 + 开始时间) - 并且您知道这是 8 小时轮班,只需在开始时间上增加 8 小时!它会根据需要转到第二天。
    【解决方案2】:

    我会建议:

    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:intime 1800 和 outtime 2100 可能意味着第二天。

    【讨论】:

    • 没错,例如 Intime 是 18:00:00,Outtime 是 21:00:00。这意味着Outtime是第二天,所以如何查询这个条件
    • @Gopal:这就是我的观点 - 根据您提供的列没有办法知道。您最好删除日期列,并使用 intime 和 outtime 列作为 DATETIME - 只有这样您才能确定。
    • 如果 Intime 和 Outtime 数据类型作为 Datetime 意味着,它将如何改变。你能举一些例子或想法吗?
    • @Gopal:更改 intime 和 outtime 以使用 DATETIME 数据类型意味着您在同一列中获得日期 AND 时间 - 您将 必须 指定与超时相关的日期...
    【解决方案3】:

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

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

    【讨论】:

    • 假设 Intime 是 18:00:00,Outtime 是 21:00:00。这意味着Outtime是第二天,所以如何查询这个条件。
    • 您的表格中没有足够的信息来做出决定,因为它可以是 3 小时和 27 小时工作日。
    猜你喜欢
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    相关资源
    最近更新 更多