Solved

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

Posted on 2010-11-23
9
3,193 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to simplify my SQL statement? 14 50
SQL 2012 R2 Express report problem 2 83
passing parameters to sql script oracle 4 26
Shredding xml into an oracle 11g Database 2 24
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…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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 how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.

914 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

20 Experts available now in Live!

Get 1:1 Help Now