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

x
?
Solved

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

Posted on 2006-07-01
6
Medium Priority
?
1,241 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
[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
  • 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
Independent Software Vendors: 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 600 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

661 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