JEPAAB
JEPAAB

Reputation: 166

SQL Server : IF Condition in WHERE clause

I have a bit parameter

@IsInRetry

If it is false I have to set the where condition to

attempts = 0

else if it is true I have to set the where condition to

attempts > 0

How could this be done?

Upvotes: 1

Views: 79

Answers (4)

Thinkeye
Thinkeye

Reputation: 655

Try this, should work also for negative attempts :-)

@IsInRetry = (attempts > 0)

Greetings.

Upvotes: 0

Jesuraja
Jesuraja

Reputation: 3844

Try this:

WHERE (attempts = @IsInRetry or (@IsInRetry = 1 and attempts > 0))

Upvotes: 0

Thanos Markou
Thanos Markou

Reputation: 2624

This:

if @IsInRetry = 0x0
   BEGIN    
      SELECT * FROM dbo.tbl 
      WHERE attempts = 0
   END
ELSE    
   BEGIN 
      SELECT * FROM dbo.tbl 
      WHERE attempts = 1
   END

Upvotes: -1

Robert
Robert

Reputation: 25763

Try this way:

( (@IsInRetry = 0 and attempts = 0) or (@IsInRetry = 1 and attempts > 0) )

Upvotes: 4

Related Questions