tags:

views:

630

answers:

3

If I have table with a Date column called "myDate", with values like "2009-08-25 09:00:09.0". I want to select all rows for Aug 25, from 12:00:01 AM until 11:59:59PM and NOTHING for Aug 26. Is it sufficient to simply use the condition:

where myDate between Date '2009-08-25' and Date '2009-08-26'

And I want to select all rows BEFORE Aug 25, not including Aug 25. Can I do:

where myDate < Date '2009-08-25'
+2  A: 

Hi Saobi,

if you want data for the full day 25 and excluding all the 26, you need to remove the first second of the 26:

where myDate >= Date '2009-08-25' and myDate < Date '2009-08-26'

or

where myDate between Date '2009-08-25' and Date '2009-08-26' - interval '1' second

Update -- A little precision: In Oracle the DATE datatype is used both for 'Date' types with or without time. If you're dealing with dates without time, it is usually stored with a 00:00 time segment, meaning midnight. Because of this, the first between (my_date between '2009-08-25' and '2009-08-26') will potentially select two full days.

By removing the first second of the 26, you ensure that you won't inadvertently select rows from the 26.

Vincent Malgrat
What do you mean by Beginning of 26. If you don't include - 1/24/60/60 in your second query, what problem may it have?
Saobi
the 26 starts at 00:00, so if you want to exclude ALL the 26, you have to explicitly remove that second
Vincent Malgrat
Yes, it includes ONLY 8-26 12:00:00 AM. Not a minute after. The chances of a row happening exactly on midnight is low, so that's ok?
Saobi
+1 and you might want to consider the more verbose interval '1' second notation, instead of 1/24/60/60.
Rob van Wijk
@Saobi: I updated my answer: sometimes dates are stored without the time information (because it is irrelevant or unknown), in that case they are stored with a '12:00:00 AM' time segment. This is why it is safer to remove that first second
Vincent Malgrat
@Rob: I agree Rob, it's clearer
Vincent Malgrat
Wait, if the dates are stored without time (12:00:00 AM), thenwhere myDate between Date '2009-08-25' and Date '2009-08-26' will not include the 08-26 , right?
Saobi
@Saobi: if the dates are stored without time, your between will select both the 08-25 and the 08-26
Vincent Malgrat
What's the query to check if any dates contains midnight?
Saobi
If you want to check if the time component of a date contains midnight, you can search with this: "WHERE trunc(mydate) = mydate", you could also check that the time is 00: "WHERE to_char(mydate, 'hh24miss') = '000000'"
Vincent Malgrat
A: 
SELECT   *
FROM   TABLE
 WHERE   myDate BETWEEN myDate  ('08/25/2009', 'MM/DD/YYYY')
                         AND  myDate  ('08/26/2009', 'MM/DD/YYYY')
caddis
A: 

To get everything between Aug 25, at 12:00:01 AM until 11:59:59 PM, inclusive, try this:

  Where myDate Between to_Date('yyyymmddhh24miss', '20090825000001') 
                  And  to_Date('yyyymmddhh24miss', '20090825235959')

(Why are you excluding midnight on the 25th ?? That first second (00:00:00) is part of the 25th as well... )

To get everything before Aug 25, try this:

Where myDate < to_Date('yyyymmdd', '20090825')
Charles Bretana