I'm having some troubles finding a solution to my SQL-Problem. I've tried google but so far my search didn't give me any statisfactory results.
I have an SSRS report with two parameters:
@SupplierId NVARCHAR (May contain NULL)
@EmployeeId NVARCHAR (May contain NULL)
My original query retrieved all the employees who came in service during the last year:
SELECT Name, Surname from dbo.Employee Where Employee.DateInService > DATEADD(year,-1,GETDATE())
Right now i want to add those parameters to the query using the following logic.
Remark this is pseudo SQL:
SELECT
...
FROM ...
WHERE Employee.DateInService > DATEADD(year,-1,GETDATE()) AND
IF (LEN(RTRIM(@SupplierId)) = 0 Or @SupplierID IS NULL ) THEN
dbo.Employee.EmployeeId = @EmployeeId
Else
dbo.Employee.SupplierId = @SupplierId
My search sofar led me to the Case
statement. I made a query which contains an syntax error (obviously). My base query:
SELECT
...
FROM ...
WHERE Employee.DateInService > DATEADD(year,-1,GETDATE()) AND
CASE WHEN (LEN(RTRIM(@SupplierId)) = 0) THEN
dbo.Employee.EmployeeId = @EmployeeId
Else
dbo.Employee.SupplierId = @SupplierId
Error: Syntax error near '='.
Question 1: Why does he give an error near the '='?
Question 2: How do i correctly implement the following:
CASE WHEN (LEN(RTRIM(@SupplierId)) = 0 "Or @SupplierId is null" ) THEN
Instead of
CASE WHEN (LEN(RTRIM(@SupplierId)) = 0) Then dbo.Employee.EmployeeId = @EmployeeId
WHEN (@SupplierId IS NULL) Then dbo.Employee.EmployeeId = @EmployeeId
ELSE dbo.Employee.EmployeeId = @EmployeeId END
Note: if i've missed a post during my google searches, please don't hesitate to point it out.
Thanks for your help