0

In this answer, it's suggested to use teh following syntax for matching against a given date, all day long.

select * from Info
  where DateColumn 
    between '2014-08-25 00:00:00' 
    and '2014-08-25 23:59:59'

Besides the fact that we're missing the last second of each day, which practically perhaps isn't a large issue but principally might be a deal-breaker, I don't see why not use the simple expression below.

The matching is done with on a semi-open interval with the upper bound being exclusive (i.e. fromAndInclusive <= date < toButNotInclusive) and a date without any time specified is assumed to be at midnight (i.e. 00:00:00.000).

select * from Info
  where DateColumn 
    between '2014-08-25' and '2014-08-26'

Please note that I'm not even close to be claiming any level of competence when it comes to SQL so this question shouldn't be been as pointing out any errors. I'm cocky otherwise but when it comes to DBs, I've been humbled once or twice. :)

17
  • 2
    @Zane It would be better to use: < '20140826'
    – Lamak
    Aug 27, 2014 at 16:08
  • 2
    Use what @Lamak said, WHERE DateColumn >= '20140825' AND DateColumn < '20140826'. See this: What do BETWEEN and the devil have in common? Aug 27, 2014 at 16:11
  • 1
    @Zane Yeah, but then you might negate the possibility of using an index on that column
    – Lamak
    Aug 27, 2014 at 16:14
  • 2
    No, it's not. You are wrong. BETWEEN is inclusive, always. See the SQL-Fiddle. Aug 27, 2014 at 16:22
  • 1
    @KonradViltersten It's not about "thinking" that it might be incorrect, BETWEEN is inclusive, it says so in the documentation: "BETWEEN returns TRUE if the value of test_expression is greater than or equal to the value of begin_expression and less than or equal to the value of end_expression."
    – Lamak
    Aug 27, 2014 at 16:27

2 Answers 2

3

Both will return incorrect results.

Between is inclusive at both ends so your first query

select * from Info
  where DateColumn 
  between '2014-08-25 00:00:00' 
  and '2014-08-25 23:59:59'

will ignore anything in the last second of 2014-08-25 as you point out.

The second query you propose is too greedy. It will include anything at exactly midnight on the 26th i.e. 2014-08-26 00:00:00.

select * from Info
  where DateColumn 
  between '2014-08-25' and '2014-08-26'

To get the results you need you should use >= and < (as @Lamak) points out in the comments:

select * from Info
  where DateColumn >= '2014-08-25' and DateColumn < '2014-08-26'.

Given the following data in the table:

2014-08-25 23:59:59
2014-08-25 23:59:59.500
2014-08-26 00:00:00
2014-08-26 00:00:00.500
2014-08-26 00:00:01

The first query matches just:

2014-08-25 23:59:59.000

The second matches:

2014-08-25 23:59:59.000
2014-08-25 23:59:59.500
2014-08-26 00:00:00.000 --this is wrong!

and finally the third (correctly) matches:

2014-08-25 23:59:59.000
2014-08-25 23:59:59.500
0
-1
select * from Info
  where DateColumn >='2014-08-25 00:00:00' 
    and datecolumn<='2014-08-25 23:59:59'
4
  • Not sure what you mean. My question is why not use the syntax. Also, you seem to have the same error that I've pointed out in my question. Would you like to check it again, please? Aug 27, 2014 at 16:07
  • What error are you referring to? I'm a bit unclear on what you're trying to solve, if not just looking for another way to do the same query?
    – Kritner
    Aug 27, 2014 at 16:11
  • 1
    @Kritner He's missing e.g. 23:59:59.333. His query is equivalent to the first one. But the second one, that I am suggesting constituted an interval which is one second longer. His interval covers 86399 seconds, while mine stretches over 86400 seconds. Aug 27, 2014 at 16:12
  • 1
    Why is this answer still here and why is is upvoted? It doesn't answer the question, it only has the same query as the OP has tried (and knew it's wrong). Aug 27, 2014 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.