Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql query - true => true, false => true or false

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 :D

like image 235
Tabloo Quijico Avatar asked Dec 07 '22 07:12

Tabloo Quijico


1 Answers

The answer column = 1 or @bit = 0 works if column may only be 0 or 1. If column may be any value you want: column = 1 or @bit = 0 and column = 0.

like image 198
Adam Luter Avatar answered Dec 27 '22 11:12

Adam Luter