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
Solved

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

Posted on 2006-07-01
6
1,221 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
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 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

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

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…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
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…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

792 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