I read about using the CASE expression inside the WHERE clause here:
http://scottelkin.com/sql/using-a-case-statement-in-a-sql-where-clause/
I’m trying to use this to filter results from my select statement, based on a contract number which will be passed in by the user’s application. My code currently throws an error of ‘Invalid parameter’ no matter what is passed in. I verified SELECT/FROM are working fine, as where as a WHERE clause without a CASE expression. Here is my code.
WHERE (CASE WHEN @ContractNo = 0 THEN @ContractNo ELSE @ContractNo END = tblContracts.ContractNo)
The redundancy of the code is for troubleshooting purposes, I’m planning on using wildcard filtering on the CASE later. I’m focusing on getting the syntax down right now. I believe this should return all records for which the parameter matches the contract number stored in the table. Any help or advice would be greatly appreciated.
After reading your explanation, there’s a better way to do this without
CASE:This will return only matching contract numbers, unless
@ContractNois 0, in which case it will return all records.Edit: I’ve just noticed that casperOne proposed the same thing. I didn’t see that. Big up yourself.