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
 General SQL Server Forums
 New to SQL Server Programming
 WHERE CASE

Author  Topic 

craigmacca
Posting Yak Master

142 Posts

Posted - 2008-02-21 : 14:14:00
Hi i have a select statement which if the parameter @SubCatID = 0

then i need this where clause

WHERE St_Cat_ID = @SubCatID AND

@StockCode LIKE '%' + @Code + '%'

else i need this where clause

WHERE St_Cat_ID = @SubCatID AND @StockCode LIKE '%' + @Code + '%'

how do i go about doing this

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2008-02-21 : 15:47:55
I'm confused. Both WHERE clauses look the same to me...?
Go to Top of Page

craigmacca
Posting Yak Master

142 Posts

Posted - 2008-02-21 : 15:55:50
o yes sorry


if @SubCatID = 0 then

WHERE StockCode LIKE '%' + @Code + '%'

else i need this where clause

WHERE St_Cat_ID = @SubCatID AND StockCode LIKE '%' + @Code + '%'
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2008-02-21 : 16:05:42
If St_Cat_ID's are not equal to 0 then you can do something like this:
WHERE
(@SubCatID = 0 OR St_Cat_ID = @SubCatID)
AND StockCode LIKE '%' + @Code + '%'
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-22 : 01:08:24
More points on the same
www.sommarskog.se/dyn-search.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -