Bernard Polman
Bernard Polman

Reputation: 855

SQL if-else equivalent in WHERE clause

I have declared a variable 'LessThan' that can either be 0 or 1. Depending on that value, I need to show results that have Amount value less than 20 or greater than 20. Here is what I have so far:

WHERE (@ResidentID = ds.ResidentID OR @ResidentID IS NULL AND ds.Amount < 20)

What I want is to show results where ds.Amount is less than < 20 IF 'LessThan' has value = 0 and I want results where ds.Amount is greater than > 20 if 'LessThan' has value = 1.

I reckon this is easy but I never really worked with SQL before and I'm having syntax troubles.

Upvotes: 1

Views: 126

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269743

You want something like this:

where ((@isLessThan = 0 and amount > 20) or (@isLessThan = 1 and amount < 20))

Upvotes: 3

Related Questions