Conditional where clause

Hi, I'm sure I have had a solution to this problem before but I just can't find it. Anyway, I want to only include certain parts of a where clause if the data I am testing is of a cetain value. The best way for me to explain it is to include some code (that doesnt work but should help).

Where
      ((If @JobsIndustryiD > 0) JobsIndustryiD = @JobsIndustryiD)
And
      ((If @JobVerticalMarketiD > 0) JobVerticalMarketiD = @JobVerticalMarketiD)
And
      ((If Len(@JobVerticalMarketiD) > 0) JobVerticalMarketiD = @JobVerticalMarketiD)
AbeSpainAsked:
Who is Participating?
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.

ee_rleeCommented:
you can write it like this:

Where
      ((@JobsIndustryiD <= 0) OR (JobsIndustryiD = @JobsIndustryiD))
And
      ((@JobVerticalMarketiD <= 0) OR (JobVerticalMarketiD = @JobVerticalMarketiD))
And
      ((Len(@JobVerticalMarketiD) <= 0) OR (JobVerticalMarketiD = @JobVerticalMarketiD))

0
Aneesh RetnakaranDatabase AdministratorCommented:
Hello AbeSpain,

Where
      ( ( @JobsIndustryiD > 0)  AND JobsIndustryiD = @JobsIndustryiD) )
And
      (( @JobVerticalMarketiD > 0)  AND JobVerticalMarketiD = @JobVerticalMarketiD) )
And
      (( Len(@JobVerticalMarketiD) > 0)  AND  JobVerticalMarketiD = @JobVerticalMarketiD)  )





Aneesh R
0
TextReportCommented:
You can do away with most of your ( ) and you dont need to use teh LEN function but you may need to use ISNULL(@JobVerticalMarketiD,'')

WHERE
      (@JobsIndustryiD <= 0 OR JobsIndustryiD = @JobsIndustryiD)
AND
      (@JobVerticalMarketiD <= 0 OR JobVerticalMarketiD = @JobVerticalMarketiD)
AND
      (@JobVerticalMarketiD = '' OR JobVerticalMarketiD = @JobVerticalMarketiD)

Cheers, Andrew
0

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
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Smart_ManCommented:
you can try select inside the where.

figure out your best choice.

waiting for your reply.
0
AbeSpainAuthor Commented:
All the above seem fine, they are all pretty much the same. Will come back shortly to close the question.
0
ee_rleeCommented:
@AbeSpain hi. just want to know, did my solution not work?
0
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
Query Syntax

From novice to tech pro — start learning today.