Simple query, possibly impossible but I know there are some clever people out there 🙂
Given a boolean parameter, I wish to define my where clause to either limit a certain column’s output – or do nothing.
So, given parameter @bit = 1 this would be the result:
where column = 1
given parameter @bit = 0 this would be the result:
where column = 1 or 0
i.e. have no effect/show all results (column is a bit field)
I’m not wanting dynamic sql – I can settle for fixing this in code but I just wondered if there’s some clever magic that would make the above neat and simple.
Is there? I’m using sql server.
cheers 😀
The answer
column = 1 or @bit = 0works if column may only be 0 or 1. If column may be any value you want:column = 1 or @bit = 0 and column = 0.