jr dones
jr dones

Reputation: 29

SELECT Date and Time in SQL 10PM up to 6AM Everyday

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

but i get nothing ...

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

2pm to 10pm is working but for 10pm to 6am its not working...TIA

Upvotes: 0

Views: 3059

Answers (4)

jr dones
jr dones

Reputation: 29

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

Upvotes: 0

Mohideen bin Mohammed
Mohideen bin Mohammed

Reputation: 20186

use OR,

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

Upvotes: 0

Chuck
Chuck

Reputation: 1031

You need to add one to the date:

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)

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1270523

You need OR in the first query:

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

Upvotes: 1

Related Questions