Solved

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

Posted on 2006-07-01
6
1,230 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
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…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

710 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