Proper way to create conditional where

I want to use one set of conditionals for a simple search, and another set for a complex search.

//pseudo code

if @advsearch = 1
WHERE column1 = searchcrit1 AND column2 = searchcrit2 AND column3 = searchcrit3
else
WHERE column1 = searchcrit OR column2 = searchcrit OR column3 = searchcrit


Advanced search means you are given 1..Nth criteria that correspond to 1..Nth columns in the table.  So it would be something like "bring back all records where name like 'nib' and address like '123' and state is california.

Whereas the simple search means you are given ONE criteria and you match it against 1..Nth columns to see if any of them contain the information.  So it would be something like "bring back all records where name is like 'nib' or address is like 'nib' or city is like 'nib'.
LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark ElySenior Coldfusion DeveloperCommented:
WHERE column1 LIKE %searchcrit1% OR
            column2 LIKE  %searchcrit2% OR
          column3 LIKE %searchcrit3%


If all three searchcrit require a like match then

WHERE column1 LIKE %searchcrit1% AND
            column2 LIKE  %searchcrit2% AND
          column3 LIKE %searchcrit3%
Jim HornMicrosoft SQL Server Data DudeCommented:
CASE blocks do not work well in a WHERE clause, so give this a whirl...
WHERE
  ( @advsearch = 1 AND column1 = searchcrit1 AND column2 = searchcrit2 AND column3 = searchcrit3) 
OR 
  ( @advsearch <> 1 AND (column1 = searchcrit OR column2 = searchcrit OR column3 = searchcrit)) 

Open in new window


This will likely result in longer processing time, so it might not be a bad idea to split your SP into two SP's, one for the @advsearch=1 and one for everything else, and construct the WHERE clauses accordingly.

Good luck.

<edited after first post>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.