SQL 10PM中的SELECT日期和时间直到每天早上6点

时间:2017-07-31 01:07:08

标签: sql sql-server vb.net datetime

 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

4 个答案:

答案 0 :(得分:1)

第一个查询中需要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)

答案 1 :(得分:0)

使用OR

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

答案 2 :(得分:0)

您需要在日期中添加一个:

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)

答案 3 :(得分:0)

SELECT scandate  来自FPI  什么是scandate BETWEEN&#39; 2017-07-20&#39;和&#39; 2017-07-25&#39;和       (DATEPART(小时,scandate)&gt; = 22或DATEPART(小时,scandate)&lt; 6)