Sep 1, 2006 #1 ice7899 Programmer Joined May 14, 2006 Messages 59 Location GB Hello I'm converting a coldfusion query to a stored procedure The Coldfusion query runs something like this: SELECT Column1 WHERE Column2 = 0 <cfif someVar = 3> AND Column3=0 </cfif> How can I achieve the selective inclusion of AND clauses in TSQL ?
Hello I'm converting a coldfusion query to a stored procedure The Coldfusion query runs something like this: SELECT Column1 WHERE Column2 = 0 <cfif someVar = 3> AND Column3=0 </cfif> How can I achieve the selective inclusion of AND clauses in TSQL ?
Sep 1, 2006 1 #2 gmmastros Programmer Joined Feb 15, 2005 Messages 14,912 Location US Code: SELECT Column1 From Table WHERE Column2 = 0 And ( (@SomeVar = 3 And Column3 = 0) Or IsNull(@SomeVar, 0) <> 3 ) Use OR. In this case, if @SomeVar = 2 then IsNull(@SomeVar, 0) <> 3 will return true, so the record will be included whether column3 = 0 or not. Does this make sense? -George Strong and bitter words indicate a weak cause. - Fortune cookie wisdom Upvote 0 Downvote
Code: SELECT Column1 From Table WHERE Column2 = 0 And ( (@SomeVar = 3 And Column3 = 0) Or IsNull(@SomeVar, 0) <> 3 ) Use OR. In this case, if @SomeVar = 2 then IsNull(@SomeVar, 0) <> 3 will return true, so the record will be included whether column3 = 0 or not. Does this make sense? -George Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
Sep 1, 2006 Thread starter #3 ice7899 Programmer Joined May 14, 2006 Messages 59 Location GB Thanks Upvote 0 Downvote