1

Is it possible to do something like this?

select * from table where Date BETWEEN '2019-05-29' AND '2019-05-29'
Roly
  • 161
  • 1
  • 5
  • 18
  • 2
    This is equivalent to `where Date ='2019-05-29'` – forpas Jun 17 '19 at 15:03
  • Already answered in this [question ](https://stackoverflow.com/questions/5125076/sql-query-to-select-dates-between-two-dates) – Pedro Mariz Jun 17 '19 at 15:04
  • I suspect a different question along the lines of I want a general query on date range where sometimes the date range will be the same date. – P.Salmon Jun 17 '19 at 15:11
  • This assumes that the datatype of `Date` is `DATE`! If it is `DATETIME`, then you are checking only for midnight! – Rick James Jun 17 '19 at 22:41

3 Answers3

2

Yes it is possible. If you have time part you could use DATE function to skip it:

SELECT * FROM table WHERE DATE(Date) BETWEEN '2019-05-29' AND '2019-05-29'
-- it may degrade performance, condition is not SARGable
Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
2

Yes, but the better approach is:

select t.*
from table t
where t.Date >= date('2019-05-29') AND 
      t.Date < date('2019-05-29') + interval 1 day

Why is this better? It doesn't have a function on the column name, so it can make use of an index on the date column.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

Yes you can, if you want to run it in a test window without manually changing the date within the code you can set it as a variable. Use trunc to get rid of time i.e there will be no 29-05-2019 23:59:00. If you want the same date within a time period remove the trunc and then you can set hours-minutes-seconds which makes your query more precise

SELECT t.*
FROM table t
WHERE t.date BETWEEN trunc(to_date(:datefrom, 'dd.mm.yyyy hh24:mi:ss')) AND
                     trunc(to_date(:dateto, 'dd.mm.yyyy hh24:mi:ss'))
Genx505
  • 50
  • 8