Link to home
Start Free TrialLog in
Avatar of digitalwise

asked on

SQL Where statement and conditional selecting

I have two fields in a table - yearcon and yearren.    

select yearren, yearcon from property

Open in new window

If yearren is populated I want to look at that field in the where statement and if not, look at the yearcon field.

where if(yearren > 0, yearren, yearcon) > 2000

Open in new window

Can someone help me?   I have the flu and my brain doesn't work!
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avoid using ISNULL in a WHERE or JOIN because it makes the columns "nonsargable".  In this case, the OR may still cause performance issues, but the rule about not using ISNULL is still correct.

WHERE ((yearren > 0 AND yearren > 2000) OR ((yearren IS NULL OR yearren = 0) AND yearcon > 2000))