Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

*** Need BETWEEN Dates to be inclusive in ORACLE PL/SQL

Posted on 2010-11-23
9
Medium Priority
?
3,859 Views
Last Modified: 2013-12-07
Hi,
I have a table with a date/time column that I need to query from.

When I use:

WHERE CALL_DATE BETWEEN TO_DATE('10/11/2010', 'MMDDYY) AND TO_DATE('10/12/2010', 'MMDDYY)

The results only bring back 10/11/2010.  I need the two date parameters to be inclusive.

Please help.  Thanks!

0
Comment
Question by:dunkin1969
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 14

Expert Comment

by:ajexpert
ID: 34198132
Date format is not correct
TO_DATE('10/11/2010', 'MM/DD/YYYY) AND TO_DATE('10/12/2010', 'MM/DD/YYYY)

Open in new window

0
 
LVL 35

Accepted Solution

by:
johnsone earned 1900 total points
ID: 34198170
I believe you want something like this:
WHERE CALL_DATE BETWEEN TO_DATE('10/11/2010', 'MM/DD/YYYY) AND TO_DATE('10/12/2010', 'MM/DD/YYYY) + (86399/86400)

Open in new window

0
 
LVL 14

Expert Comment

by:ajexpert
ID: 34198174

SELECT * FROM <YOURTABLE>
WHERE CALL_DATE BETWEEN TO_DATE('10/11/2010', 'MM/DD/YYYY') AND TO_DATE('10/12/2010', 'MM/DD/YYYY')

Open in new window

0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 

Author Comment

by:dunkin1969
ID: 34198228
ajexpert,
your code doesn't return the desired results.  Its excludes the 10/12/2010 records.

johnsone,
your code works!!!  can you explain to me the significance of      + (86399/86400)   ?

Thanks !!!
0
 
LVL 35

Assisted Solution

by:johnsone
johnsone earned 1900 total points
ID: 34198261
86400 is the number of seconds in 1 day.  86399 is one less than that.  Since you are not specifying a time in your date it defaults to midnight.  By adding 86399/86400 to a date at midnight, that takes us to 23:59:59 on that day and would therefore include all times in that date.  Your original query was only including 1 second of 10/13/2010, the 1 second that occurred at midnight.
0
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 100 total points
ID: 34198263
dates are down to the second.


TO_DATE('10/12/2010', 'MM/DD/YYYY')  is 2010-10-12 00:00:00

so,  you're excluding all seconds from 00:00:01  to 23:59:59.

date math is in units of days.  86399/86400 is 23 hours, 59 minutes 59 seconds
0
 

Author Comment

by:dunkin1969
ID: 34198276
Thanks for the explanation johnsone and sdstuber.  Makes perfect sense and I actually understand it!
0
 

Author Comment

by:dunkin1969
ID: 34198475
Yikes,
I forgot to ask how to adjust the code to qualify for a SINGLE date (10/11/2010)....

This uses the BETWEEN suntax:

WHERE CALL_DATE BETWEEN TO_DATE('10/11/2010', 'mm/dd/yyyy') AND TO_DATE('10/11/2010', 'mm/dd/yyyy') + (86399/86400)

But how can I utilize this format to include the hours/seconds? :

WHERE CALL_DATE = TO_DATE('10/11/2010', 'mm/dd/yyyy')

Thanks!!
Maybe I'll post to a new question if I can't get responses here.
0
 
LVL 35

Expert Comment

by:johnsone
ID: 34198667
For a single date use the same between syntax:

WHERE CALL_DATE BETWEEN TO_DATE('10/11/2010', 'mm/dd/yyyy') AND TO_DATE('10/11/2010', 'mm/dd/yyyy') + (86399/86400)

If you do not want to use the between syntax, then you are going to have to truncate your date column to remove the time:

WHERE TRUNC(CALL_DATE) = TO_DATE('10/11/2010', 'mm/dd/yyyy')

However, be aware that this will not allow the use of an index on CALL_DATE (if one exists).  To get around this you would have to create a function based index on the truncated column.  Not difficult, but it is another index to maintain.

0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question