We help IT Professionals succeed at work.

SQL Where statement and conditional selecting

45 Views
Last Modified: 2016-03-15
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!
Comment
Watch Question

Database Engineer
CERTIFIED EXPERT
Top Expert 2005
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Scott PletcherSenior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
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))
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.