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 2012 Forums
 Transact-SQL (2012)
 CASE or IF statement in where clause

Author  Topic 

ArnoldG
Starting Member

36 Posts

Posted - 2013-12-05 : 09:56:27
Hello, I am struggling with this:
I want to conditionally filter in the WHERE clause.

When I would normally use WHERE, it would be:

WHERE [Brand]  = ‘_PARAM1_’


But I want to conditionally filter [Brand], OR have all the values of [Brand]

Something like:

(
IF 1=1
THEN [Brand] = '_PARAM1_'
ELSE 'Give me all values'
END
)


How do I properly translate that into a where clause ?
Thx,
Arnold

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2013-12-05 : 10:24:14
one way:
WHERE ([Brand] = '_PARAM1_' OR 1=1)
AND ...

Be One with the Optimizer
TG
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-12-05 : 12:12:25
Perhaps you are trying to implement a "catch-all query?"

http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/
Go to Top of Page

ArnoldG
Starting Member

36 Posts

Posted - 2013-12-05 : 12:30:12
Thanks TG,
I did to get this kind of logic, but it works fine.

Also thank you Lamprey for your input. It explains a lot.

Arnold
Go to Top of Page
   

- Advertisement -