Question : 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)

Answer : Conditional where clause

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
Random Solutions  
 
programming4us programming4us