Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntax/Logic Trouble in Where Clause

Status
Not open for further replies.

kramerd1506

Technical User
Jul 18, 2003
20
0
0
US
Hi folks, you're all so helpful that I just had to come back.

Here's the code I'm working with:

select distinct
....
from
table a
table b
(select to_char((sysdate), 'dy') from dual) dt
where
....
AND
if dt = 'fri'
then

((a.date1 >= trunc(sysdate+1)
and a.date1 < trunc(sysdate+4))
OR
(b.date2 >= trunc(sysdate+1)
AND b.date2 < trunc(sysdate+4)))

else

((a.date1 >= trunc(sysdate+1)
and a.date1 < trunc(sysdate+2))
OR
(b.date2>= trunc(sysdate+1)
AND b.date2 < trunc(sysdate+2)))

AND
....

When I run this I get an error at "if dt = 'fri'" of Invalid Relational Operator.

Any advice? Thanks in advance, as always.
 
Kramer,

In Oracle SQL, we cannot use "IF" as a relational invocation; we must stay within the "WHERE...AND...OR..." convention. Also, as a code optimisation, we can use the "BETWEEN" operator instead of the construct you have used. (Note: the BETWEEN operator includes the two endpoints, but since you have used TRUNC(SYSDATE+...), you will still get the effect you wish). Here is the code rewrite that should work for you:
Code:
select distinct
....
from
table a
table b
(select to_char((sysdate), 'dy') from dual) dt
where
....
AND (  (dt = 'fri' AND
        (a.date1 BETWEEN trunc(sysdate+1) AND trunc(sysdate+4))
        OR
        (b.date2 BETWEEN trunc(sysdate+1) AND trunc(sysdate+4))
       )
OR  (   a.date1 BETWEEN trunc(sysdate+1) AND trunc(sysdate+2)
        OR 
        b.date2 BETWEEN trunc(sysdate+1) AND trunc(sysdate+2)
    )
    )
AND ....
Let us know if this is helpful.

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)

Do you use Oracle and live or work in Utah, USA?
Then click here to join Utah Oracle Users Group on Tek-Tips.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top