Nikola Stanković
Nikola Stanković

Reputation: 881

if in partial string of an SQL statement?

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

Upvotes: 2

Views: 113

Answers (1)

podiluska
podiluska

Reputation: 51494

You can use

  WHERE MyTable.MyColumn = @MyVariable
  AND ((@date is null) or (MyColumn2 = @MyVariable2))

Upvotes: 1

Related Questions