2

I am coding a Table-valued Function in MS SQL 2008 R2.

Is it possible for a partial string of an SQL statement, which is in the WHERE clause to wrap an IF construct?

WHERE MyTable.MyColumn = @MyVariable
      AND (MyColumn2 = @MyVariable2)
ORDER BY MyTable.MyColumn DESC
3
  • 1
    It's not clear what you are asking. If you add table structure, sample data and expected output it would be a lot easier to see what you want to do. Commented Nov 27, 2012 at 8:29
  • I want to pack the "AND (@ MyVariable2 MyColumn2 =)" in an IF construct. And the IF constuct is comparing a value with a defined variable outside of the statement. Do you know now what I mean? Commented Nov 27, 2012 at 8:41
  • Outside I have got a variable @Date and if this variable is not null I would like to expand my Where clause with the partial string "AND (MyColumn2 = @MyVariable2)" Commented Nov 27, 2012 at 8:56

1 Answer 1

1

You can use

  WHERE MyTable.MyColumn = @MyVariable
  AND ((@date is null) or (MyColumn2 = @MyVariable2))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.