I have a situation where if an input variable is equal to a certain string, I want to evaluate a condition, and if it's not, I don't care about that condition.
I've tried to do it with both IF and CASE but get incorrect syntax errors. Maybe this isn't possible or maybe I just don't have all the parentheses in the right places?
Declare @state nvarchar(255) = 'KY'
,@group nvarchar(255) = 'COM'
SELECT * from business_auth
where (case when @group = 'COM'
then (@state = AddressState or AddressState is null)
else 1=1
end)
I want it to look at the state column if the group is COM but for any other group, I don't care about the state. I have a long list of other AND evaluations as well here, so I'd prefer not to have to re-write the whole thing or put the IF before the query, and have two versions of the query in the procedure, one evaluating state and one not doing so. It seems like there would be a more eloquent solution, but if there's not, I'll go with that.