SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Dynamic WHERE clause in SELECT satment?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Eagle_f90
Constraint Violating Yak Guru

USA
424 Posts

Posted - 06/21/2013 :  14:27:40  Show Profile  Visit Eagle_f90's Homepage  Reply with Quote
I have a SELECT statement with 2 conditions in the WHERE clause and I want 1 of those conditions to be optional as the value may or may not be passed in by the calling application. Doing some goggling I found this solution:

WHERE di.InventoryDate BETWEEN @SDate AND @EDate
      AND di.JDELocation = @JDELocation OR @JDELocation IS NULL


Which works, sort of. As long as the JDELocation is passed in the results returned will always be between the Sdate and EDate but if the JDELocation is not passed in by the calling app then the date constraint is ignored. How can I modify this so that even if the JDELocation is not passed in the date constraint is still followed?

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia

TG
Flowing Fount of Yak Knowledge

USA
6062 Posts

Posted - 06/21/2013 :  14:29:50  Show Profile  Reply with Quote
WHERE di.InventoryDate BETWEEN @SDate AND @EDate
AND (di.JDELocation = @JDELocation OR @JDELocation IS NULL)

Be One with the Optimizer
TG
Go to Top of Page

Eagle_f90
Constraint Violating Yak Guru

USA
424 Posts

Posted - 06/21/2013 :  14:33:11  Show Profile  Visit Eagle_f90's Homepage  Reply with Quote
Thanks, that worked perfectly

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 06/21/2013 :  14:33:43  Show Profile  Reply with Quote
http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.19 seconds. Powered By: Snitz Forums 2000