I'm writing a SQL query where I want to use CASE statement in WHERE Clause.
My logic goes like below:
If
MemberStatusId = 1, thenCollaborator.Unavailability = @NullDateIf
MemberStatusId = 2, thenCollaborator.Unavailability = @LastDateElse
Collaborator.Unavailability > @NullDateAND thenCollaborator.Unavailability < @LastDate.
First attempt:
{Collaborator}.[UnavailabilityEndDate] =
CASE
WHEN @MemberStatusId = 1 THEN @NullDate
WHEN @MemberStatusId = 2 THEN @LastDate
ELSE @NullDate AND @LastDate
END;
I don't know what to write in ELSE part to make check the UnavailabilityEndDate between NullDate and LastDate.
Second attempt - but this results in an error:
CASE
WHEN @MemberStatusId = 1 THEN {Collaborator}. [UnavailabilityEndDate] = @NullDate
WHEN @MemberStatusId = 2 THEN {Collaborator}.[UnavailabilityEndDate] = @LastDate
ELSE {Collaborator}. [UnavailabilityEndDate] > @NullDate AND [Collaborator].[UnavailabilityEndDate] < @LastDate
Error:
NOTE: I tried to code sample or Ctrl+K but it didn't work for me to format or style the code in the comment box.

CASEin aWHEREclause makes little sense. WithCASEyou can check a condition in order to generate a value. Why generate that value in order to check it again in theWHEREclause? Just check the condition in theWHEREclause right away instead. In your case: