DJPB
DJPB

Reputation: 5819

Tsql translation

Supose I have a table like

Id | IsActive
1  | True
2  | False

And i want to search items that are active, not active or both

I would like to have a query

declare @ActiveState bit

Select * from myTable where IsActive...

and on my where clause i would like to have something like

if(@ActiveState != null)
    myTable.IsActive == @ActiveState
else
    myTable.ActiveState == true || myTable.ActiveState == false

but i can't find a way to do this on the same where clause

tks

Upvotes: 0

Views: 89

Answers (3)

Charles Bretana
Charles Bretana

Reputation: 146603

Where IsActive = IsNull(@ActiveState, IsActive)
or
Where IsActive = Coalesce(@ActiveState, IsActive)

Upvotes: 1

The Scrum Meister
The Scrum Meister

Reputation: 30141

WHERE myTable.IsActive  = @ActiveState OR @ActiveState IS NULL

Upvotes: 1

Mark Ransom
Mark Ransom

Reputation: 308538

select * from myTable where @ActiveState is null or IsActive = @ActiveState

Upvotes: 1

Related Questions