19

How to select records between a date to another date given a DateTime field in a table.

Arulkumar
  • 12,966
  • 14
  • 47
  • 68
bala3569
  • 10,832
  • 28
  • 102
  • 146

2 Answers2

28
SELECT * 
FROM tbl 
WHERE myDate BETWEEN #date one# AND #date two#;
Shnugo
  • 66,100
  • 9
  • 53
  • 114
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 2
    between is terribly misunderstood. casting of dattime to date happens intentionally and unintentionally. Personally I like explicit >= and <= because it does not save much typing but is so much less readable. – Richard Nov 06 '19 at 03:37
  • SELECT * FROM bookings WHERE item_id=2884 AND (start_date BETWEEN '2020-09-05 23:31:45' AND '2020-09-06 00:00:00' OR end_date BETWEEN '2020-09-05 23:31:45' AND '2020-09-06 00:00:00') ) This is my code, but it doesnt work well – ExpertWeblancer Sep 05 '20 at 06:35
18
select * 
from blah 
where DatetimeField between '22/02/2009 09:00:00.000' and '23/05/2009 10:30:00.000'

Depending on the country setting for the login, the month/day may need to be swapped around.

Arulkumar
  • 12,966
  • 14
  • 47
  • 68
Tikeb
  • 978
  • 3
  • 9
  • 25