I’ve got a software application where I need to query a database based on specific criteria if in the input parameter specific strings are found. However, if the specific strings are not found then I do not want to include the limiting criteria.
Not quite sure how to approach this but I have some code already. I just cannot seem to finish it. My where clause appears as follows:
WHERE p.country = @country AND
p.[state] = @state AND
p.last_update BETWEEN @from AND @to AND
f.facility_id = @facid AND
p.sex = @gender AND
p.disease1 = CASE WHEN @inpDisease LIKE '%AIDS%' THEN 1 ELSE --?? Here is where it gets tricky
If AIDS is not found in the @inpDisease string then I do not need to limit the query with the p.disease1 criteria. Disease1 is data type bit so only 1 or 0 exists in that column. How could I specify both 0 and 1 in the ELSE or have it ignore Disease1 all together? I’ve got 15 more BIT fields that have to be checked if they exist in the inpDisease or either left out if they are not.
Any ideas what the best way to complete this might be? I don’t want to end up with a ton of SELECTs utilizing conditional logic just to omit 1 field here or 1 field there from the query.
I can’t seem to come up with a good idea to solve this issue, so any help is appreciated!
Thanks
Compare the column to its own value if you don’t want to use that criterion: