Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-07-01
6
Medium Priority
?
1,244 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
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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.  …
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 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…
Suggested Courses

876 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