Solved

Oracle SQL syntax question.... NOT BETWEEN  (select date1, date2 from test_table)

Posted on 2006-07-01
6
1,225 Views
Last Modified: 2008-02-01

Hi,
How would I get all dates above that are  NOT BETWEEN (select date1, date2 from test_table)

select dt from (select trunc (sysdate,'Y')+rownum-1 dt from dual connect by level <=366) where trunc(dt,'Y')=trunc(sysdate,'Y') AND NOT BETWEEN (select date1, date2 from test_table)


if select date1, date2 from test_table returned 3 rows

dt NOT BETWEEN '07-JUL-2006'  AND  '13-JUL-2006'
dt NOT BETWEEN '16-JUL-2006'  AND  '18-JUL-2006'
dt NOT BETWEEN '26-JUL-2006'  AND  '30-JUL-2006'

if 100 rows etc..

Thanks, Bill
0
Comment
Question by:bcarlis
  • 4
  • 2
6 Comments
 
LVL 9

Expert Comment

by:neo9414
ID: 17025226
select * from(
select dt from (select trunc (sysdate,'Y')+rownum-1 dt
from dual connect by level <=366)
where trunc(dt,'Y')=trunc(sysdate,'Y') )
where dt<(select min(date1) from test_table)
and dt>(select max(date2) from test_table);



0
 
LVL 2

Author Comment

by:bcarlis
ID: 17025251
No.. I don't think that will do it.


where dt<(select min(date1) from test_table)
and dt>(select max(date2) from test_table);

Min would be '07-JUL-2006'
Max would be '30-JUL-2006'

That would exclude  '14-JUL-2006', '15-JUL-2006',
         and  '19-JUL-2006' through  '25-JUL-2006'
           
0
 
LVL 2

Author Comment

by:bcarlis
ID: 17025327
I've got it...

select dt from (select trunc (sysdate,'Y')+rownum-1 dt from dual connect by level <=366) where trunc(dt,'Y')=trunc(sysdate,'Y') AND dt NOT IN (select n.dt from test_table tt,
(select trunc (sysdate,'Y')+rownum-1 dt from dual connect by level <=366) n
where trunc(n.dt,'Y')=trunc(sysdate,'Y') AND n.dt <= tt.date2 and tt.date1 <= n.dt )

Thanks for the help....
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:bcarlis
ID: 17026688
OK,

How do I check the intersection of two sets?

SET 1 - 1,3,5,7,9,11,15,16,21,31,41
SET 2 - 2,4,7,11,12,13,16,23,44,55

Intersection is 7, 11, 16

select num from test_table
where this makes SET 1

select num from test_table
where this makes SET 2

0
 
LVL 2

Author Comment

by:bcarlis
ID: 17026706
actonwang,

You are the one that gave me
(select trunc (sysdate,'Y')+rownum-1 dt from dual connect by level <=366) n

You'll understand the full tamale...

SET 1
select n.dt from test_table tt,
(select trunc (sysdate,'Y')+rownum-1 dt from dual connect by level <=366) n
where trunc(n.dt,'Y')=trunc(sysdate,'Y')
AND n.dt <= tt.date2 and tt.date1 <= n.dt

SET 2
 (select n.dt "Desired Dates" from
(select trunc (sysdate,'Y')+rownum-1 dt from dual connect by level <=366) n
where trunc(n.dt,'Y')=trunc(sysdate,'Y')
AND n.dt <= '06-JUL-06' and '01-JUL-06' <= n.dt )

I need to know is there ANY in SET 1 that are in SET 2 - Yes or No

I don't care at present which are in the other.. just if there is any that match....

Thanks, Bill :>)

0
 
LVL 9

Accepted Solution

by:
neo9414 earned 150 total points
ID: 17026767
just do the intersect..

select n.dt from test_table tt,
(select trunc (sysdate,'Y')+rownum-1 dt from dual connect by level <=366) n
where trunc(n.dt,'Y')=trunc(sysdate,'Y')
AND n.dt <= tt.date2 and tt.date1 <= n.dt
INTERSECT
(select n.dt "Desired Dates" from
(select trunc (sysdate,'Y')+rownum-1 dt from dual connect by level <=366) n
where trunc(n.dt,'Y')=trunc(sysdate,'Y')
AND n.dt <= '06-JUL-06' and '01-JUL-06' <= n.dt )


Example
--------------

create table test_table1 (num number);
create table test_table2 (num number);

insert into test_table1 values(1);
insert into test_table1 values(3);
insert into test_table1 values(5);
insert into test_table1 values(7);

insert into test_table2 values(1);
insert into test_table2 values(2);
insert into test_table2 values(5);
insert into test_table2 values(6);


select num from test_table1
intersect
select num from test_table2;

       NUM
----------
         1
         5

0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

740 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