ruedi ruedi - 1 year ago 89
SQL Question

Exclusive IF Statement in WHERE clause

I have the following initial query.

Select * from Table
where Datum1 < @Datum2
and Employee_Name = Employee_Name

But I want to filter
Datum1 < Datum2
only if the Employee is a member of the group students. For non student employees I do not want to filter
Datum1 < @Datum2

My first idea was

Select * from Table
where CASE WHEN Datum1 < @Datum2 END
and Employee_Name = Employee_Name

But this is not working. So I have checked other SO Solutions refering to
where clause in sql if statement
but all of them take care of problems where we have a eiter or decision like
if Student then Datum1 < @Datum2 else Datum2 < @Datum2
. But I want the statement not be considered at all if the condition is true. Anyone who can help me here?

Answer Source

You can do this with an OR statement, accounting for both of the scenarios. I'm assuming there's a field called Employee_Group that wasn't mentioned in the question, but this should point you in the right direction:

Select  *
From    Table
Where   Employee_Group <> 'Students'
    Employee_Group = 'Students'
And Datum1 < @Datum2
And     Employee_Name = @Employee_Name
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download