'sql dynamic pivot where clause failing

I am trying to figure out where my query is failing on a where clause, I hope some of the local experts could point it out and explain why. Also I would like to add a total to the end of the query but mainly need to get the where clause to work!

If I do this query by itself it works the way I want:

 select
    JobCode,
    DepartmentName,
    (COALESCE(MonTime, 0)+COALESCE(TueTime, 0)+COALESCE(WenTime, 0)+COALESCE(ThurTime, 0)+COALESCE(FriTime, 0)
                +COALESCE(SatTime, 0)+COALESCE(SunTime, 0)) as total
    
from TimeSheetTable

INNER JOIN DepartmentTable ON TimeSheetTable.DeptId=DepartmentTable.Id
inner join  JobCodeTable on TimeSheetTable.JobId=JobCodeTable.Id


where  WeekStartDate  Between '2022-04-11' and '2022-04-25'

When I add the query to be pivoted I am getting this error:

Msg 207, Level 16, State 1, Line 16 Invalid column name '2022-04-11'. Msg 207, Level 16, State 1, Line 16 Invalid column name '2022-04-11'.

The table:

CREATE TABLE [dbo].[TimeSheetTable]
(
    [Id] INT NOT NULL PRIMARY KEY IDENTITY, 
    [WeekStartDate] DATE NOT NULL, 
    [MonTime] FLOAT NULL, 
    [TueTime] FLOAT NULL, 
    [WenTime] FLOAT NULL, 
    [ThurTime] FLOAT NULL, 
    [FriTime] FLOAT NULL, 
    [SatTime] FLOAT NULL, 
    [SunTime] FLOAT NULL, 
    [EmpID] INT NOT NULL, 
    [JobId] INT NOT NULL,
    [DeptId] INT NOT NULL, 
    [Comments] NVARCHAR(MAX) NULL, 

    CONSTRAINT [FK_TimeSheetTable_JobTable] 
        FOREIGN KEY ([JobId]) REFERENCES [JobCodeTable]([Id]),
    CONSTRAINT [FK_TimeSheet_Emp] 
        FOREIGN KEY ([EmpID]) REFERENCES [EmployeeTable]([Id]),
    CONSTRAINT [FK_TimeSheetTable_Department] 
        FOREIGN KEY ([DeptId]) REFERENCES [DepartmentTable]([Id])
)

The query that throw the error:

--get row names
DECLARE 
    @departments NVARCHAR(MAX) = '', 
    @sql     NVARCHAR(MAX) = '';

-- select the category names
SELECT 
    @departments += QUOTENAME(DepartmentName) + ','
FROM 
    DepartmentTable
ORDER BY 
    DepartmentName;

-- remove the last comma

SET @departments = LEFT(@departments, LEN(@departments) - 1);

--get columns
--PRINT @columns;
-- construct dynamic SQL
SET @sql ='
SELECT * FROM 
(
   select
    JobCode,
    DepartmentName,
    (COALESCE(MonTime, 0)+COALESCE(TueTime, 0)+COALESCE(WenTime, 0)+COALESCE(ThurTime, 0)+COALESCE(FriTime, 0)
                +COALESCE(SatTime, 0)+COALESCE(SunTime, 0)) as total
    
from TimeSheetTable
INNER JOIN DepartmentTable ON TimeSheetTable.DeptId=DepartmentTable.Id
inner join  JobCodeTable on TimeSheetTable.JobId=JobCodeTable.Id
--getting error here--
where  WeekStartDate  Between "2022-04-11" and "2022-04-25"
    
) t 
PIVOT(
   sum(total)
   FOR DepartmentName IN ('+ @departments +')
)pivot_table
ORDER BY JobCode
'
---- execute the dynamic SQL
EXECUTE (@sql);

This is what I have to work with but not my design I need to get a date range and fairly new to SQL! Eventually I would like to make it into a stored procedure and put in variables in the where clause, just using the dates hard coded now



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source