John Waclawski John Waclawski - 7 months ago 6
SQL Question

Where clause depending of the value of a variable

I have 2 fields I want to send values to within a

WHERE
statement.


  • If a variable = 0 then set 2 field values to 100.

  • If the variable = 1 then set those same 2 field values to 101.



In my imaginary world, somehow this would work:

Where CASE WHEN @ReportType = 0 THEN
od.StatusCd = 100 AND odm.StatusCd = 100
WHEN @ReportType = 1 THEN
od.statusCd = 101 AND odm.StatusCd = 101
End
And od.CompletionDate between ....
And so on....


I know this is wrong. But this is where I am at right now.

Answer

If I have understood what you are trying to do, this should work :

Where 
(
(@ReportType = 0 AND od.StatusCd = 100 AND odm.StatusCd = 100)
OR
(@ReportType = 1 AND od.statusCd = 101 AND odm.StatusCd = 101)
)
And od.CompletionDate between ....
And so on....
Comments