Solved

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

Posted on 2010-11-23
9
3,117 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
  • 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 34

Accepted Solution

by:
johnsone earned 475 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 34

Assisted Solution

by:johnsone
johnsone earned 475 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 73

Assisted Solution

by:sdstuber
sdstuber earned 25 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 34

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now