Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 if statemnt in where clause

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-09-11 : 14:12:03
writes "How do I use if statemnt in where clause


here is what Im trying to do

I have 3 tables A,B,C linked by A.id
I need a query something like
select A.des,B.df_date,C.nxt_date,A.id
from A
inner join B
on A.id=B.id
inner join C
on A.id=C.id
where
(if A.des='P Sale' then B.df_date between '08/01/2002'and '08/31/2002'
elseif A.des='AFB' then C.nxt_date
between '08/01/2002'and '08/31/2002')
Please advice
Thanks

A.des B.df_date C. nxt_date A.ID
------------------------------ ---------------------------
P Sale 2002-04-15 2001-09-01 1
P Sale 2002-06-14 2001-07-01 2
P Sale 2002-06-17 2001-07-01 3
P Sale 2002-08-16 2001-02-01 5
AFB 2002-04-16 2002-04-01 6
AFB 2002-06-10 2002-02-01 9
AFB 2002-08-09 2002-09-01 7
Thanks"

LarsG
Constraint Violating Yak Guru

284 Posts

Posted - 2002-09-11 : 15:58:53
look up case expressions. Your query could be written as

select A.des,B.df_date,C.nxt_date,A.id
from A
inner join B
on A.id=B.id
inner join C
on A.id=C.id
where
case when A.des='P Sale' then B.df_date
when A.des='AFB' then C.nxt_date
else '08/01/2002' end
between '08/01/2002'and '08/31/2002'




Go to Top of Page
   

- Advertisement -