Home > Ask the Oracle Experts > SQL Questions & Answers > Using BETWEEN with DATETIMEs in SQL
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Using BETWEEN with DATETIMEs in SQL

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Oracle tips, scripts, and expert advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


>
QUESTION POSED ON: 03 July 2008
Can we retrieve data between two dates, including the two dates? For example, if I require data from 01/01/2008 to 31/03/2008, using "between" only gives the data from 02/01/2008 to 30/03/2008. Is there any way we can get the data including the dates 01/01/2008 and also 31/03/2008?

>
EXPERT RESPONSE
You may not have given enough information for me to diagnose your problem correctly. BETWEEN does include the endpoints of the range. This is the default behaviour.

What might be happening is that you actually have datetime values in your table, rather than date values. For example, let's say the values in your table are:

myDateTime
2008-01-01 01:01
2008-01-22 01:22
2008-02-01 02:01
2008-02-29 02:29
2008-03-30 03:30
2008-03-31 09:37

Now run a query with this condition:

WHERE myDateTime BETWEEN '2008-01-01' 
                     AND '2008-03-31'

You will see that the last value, 2008-03-31 09:37, is missing. This is because it is outside the range specified by the BETWEEN condition. The reason is due to fact that the values in the BETWEEN condition are converted to datetime values in order to do the comparisons, so the condition is actually:

WHERE myDateTime BETWEEN '2008-01-01 00:00:00' 
                     AND '2008-03-31 00:00:00'

Thus, all the datetime values for that last day will be missing (except for those, if any, which specify midnight exactly).

When dealing with datetimes like this, you could try to specify the upper end of the range explicitly, like this:

WHERE myDateTime BETWEEN '2008-01-01' 
                     AND '2008-03-31 23:59:59.993'

However, the problem with this is that you have to be sure you specify the actual last possible datetime value for that date, and this becomes quite tricky as it depends on which version of which DBMS you're using.

A much better approach is to abandon the BETWEEN condition, and do this instead:

WHERE myDateTime >= '2008-01-01' 
  AND myDateTime  < '2008-04-01'

Notice that the operator in the second condition is less than, not less than or equal.

The bonus here is that you don't have to waste any time calculating the last day of the month (28th, 29th, 30th, or 31st). Just make it less than the 1st of the following month. Easier to code, just as efficient, and guaranteed to get all included datetime values no matter what precision the database uses (microsecond, 3 microseconds, whatever).

Neat, eh?


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
SQL
Using the SQL date function to find aggregate totals by month
Using an SQL SELECT statement from a non-existing table
Using LEFT OUTER JOIN query to get zero row counts in SQL
How to return multiple values for THEN clause in an SQL CASE expression
Can I concatenate row values in SQL?
Should I try to avoid a LEFT OUTER JOIN in SQL?
Tips for derived tables in SQL and using FULL OUTER JOINs
How to write an SQL query for two foreign keys to the same table
How to create an SQL CHECK constraint for two letters
How to return a zero in SQL instead of no row back for a select count

Oracle development languages
Using LEFT OUTER JOIN query to get zero row counts in SQL
How to return multiple values for THEN clause in an SQL CASE expression
Can I concatenate row values in SQL?
Should I try to avoid a LEFT OUTER JOIN in SQL?
Tips for derived tables in SQL and using FULL OUTER JOINs
How to write an SQL query for two foreign keys to the same table
How to create an SQL CHECK constraint for two letters
How to return a zero in SQL instead of no row back for a select count
How to write an SQL query using GROUP BY for row analysis
Using nested SQL string functions to find ERP customer values in a table

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Oracle White Papers: Fusion Middleware
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts