0

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?

Community
  • 1
  • 1
ruedi
  • 5,365
  • 15
  • 52
  • 88

3 Answers3

3

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'
Or
(
    Employee_Group = 'Students'
And Datum1 < @Datum2
)
And     Employee_Name = @Employee_Name
Siyual
  • 16,415
  • 8
  • 44
  • 58
0

Your clause "Employee_Name = Employee_Name" is useless.

Try this :

Select * from Table where Datum1 < @Datum2 Or Category <> 'Student'

Zuperdudu
  • 1
  • 1
0

If there's an EmployeeGroup field and you want to filter on EmployeeName and add Datum to the filter only when the group is not 'Students' then this is how your query should look like:

SELECT * FROM Table
    WHERE EmployeeName = @employeeName 
    AND (Datum < @datum2 OR EmployeeGroup <> 'Students')