【问题标题】:SELECT Date and Time in SQL 10PM up to 6AM Everyday在 SQL 中选择日期和时间,每天晚上 10 点到早上 6 点
【发布时间】:2018-01-06 10:56:46
【问题描述】:
 SELECT scandate from FPI where scandate BETWEEN '2017-07-20' and '2017-07-25' DATEPART(hh,[scandate]) >= 22 AND DATEPART(hh,[scandate]) < 6 

但我什么也得不到……

  SELECT scandate from FPI where scandate BETWEEN '2017-07-20' and '2017-07-25' DATEPART(hh,[scandate]) >= 14 AND DATEPART(hh,[scandate]) < 22

下午 2 点到晚上 10 点正常工作,但晚上 10 点到早上 6 点不工作...TIA

【问题讨论】:

  • 请记住,如果您打算下一个日期早上 6 点,您应该执行 dateadd(day,scandate ,1) 然后检查。
  • 如果您不在日期中添加一个,您如何获得午夜 6 点之间的时间?
  • 它在 5 天之间,因此无需添加一天 ..

标签: sql sql-server vb.net datetime


【解决方案1】:

选择扫描日期 来自 FPI '2017-07-20' 和 '2017-07-25' 和 (DATEPART(hour, scandate) >= 22 OR DATEPART(hour, scandate)

【讨论】:

    【解决方案2】:

    使用OR

     SELECT scandate from FPI where scandate BETWEEN '2017-07-20' and '2017-07-25' DATEPART(hh,[scandate]) >= 22 OR DATEPART(hh,[scandate]) < 6; 
    

    【讨论】:

    • 它错了我确实得到了数据..但输出是 470k
    • 谢谢你们我解决了问题我只是在 datepart SELECT scandate from FPI where scandate BETWEEN '2017-07-20' and '2017-07-25' DATEPART 之间添加 OPEN 和 CLOSE () (hh ,[scandate]) >= 22 OR DATEPART(hh,[scandate])
    【解决方案3】:

    你需要在日期上加一个:

    SELECT scandate 
    FROM FPI 
    WHERE scandate BETWEEN '2017-07-20' and dateadd(day,1,'2017-07-25') AND
      (DATEPART(hour, scandate) >= 22 OR DATEPART(hour, DATEADD(day,1, scandate)) < 6)
    

    【讨论】:

      【解决方案4】:

      第一个查询需要OR

      SELECT scandate 
      FROM FPI 
      WHERE scandate BETWEEN '2017-07-20' and '2017-07-25' AND
            (DATEPART(hour, scandate) >= 22 OR DATEPART(hour, scandate) < 6)
      

      【讨论】:

        猜你喜欢
        • 2013-03-08
        • 2013-05-05
        • 2022-08-09
        • 1970-01-01
        • 2020-05-28
        • 2020-12-01
        • 2017-07-02
        • 2016-09-06
        • 2014-09-29
        相关资源
        最近更新 更多