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 2008 Forums
 Transact-SQL (2008)
 if within where

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2011-10-05 : 06:01:04
Hi,
In the sql query below, I believe i can not use if within where clause as you see below.
So, how can I place these or statements within the where calause based on if statements?

The point is, if there is no @SIA, then there should not be any records with field Pyear = @SIA
same principle for @SIC

Thanks

select
Pyear,
field2,
field3,
etc
from
tblMain
where
Pyear in ('2000', '2003', '2004')
AND qrt in (1, 2, 3)

if (len(@SIA) > 0)
begin
OR (Pyear in (@SIA))
end
if (len(@SIC) > 0)
begin
OR (Pyear in (@SIC))
end

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-05 : 06:05:46
it should be

...
where
Pyear in ('2000', '2003', '2004')
AND qrt in (1, 2, 3)
AND (len(@SIA) = 0 OR Pyear in (@SIA))
AND (len(@SIC) = 0 OR Pyear in (@SIC))


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2011-10-05 : 06:18:39
solved. Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-05 : 06:26:52
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -