Shaggy
Shaggy

Reputation: 5790

case statement in where clause depending on variable value

Depending on variable value my Where condition should change

If @CustID <> 0 Then

Where SomeColumn = @BillID

Else

Where SomeColumn In (@BillID,0)

My Query:

Select * 
From SomeTable
Where SomeColumn = (Case When @CustID <> 0 Then @BillID Else /* What to Write Here */ End)

Upvotes: 0

Views: 1626

Answers (1)

Jo&#235;l Salamin
Jo&#235;l Salamin

Reputation: 3576

Here is the query you're looking for:

SELECT *
FROM SomeTable ST
WHERE (@CustID <> 0 AND ST.SomeColumn = @BillID)
    OR (@CustID = 0 AND ST.SomeColumn IN (@BillID, 0))

Hope this will help.

Upvotes: 1

Related Questions