I have a stored procedure that needs to return a weekly schedule of items being made on different days. It takes in a start date.
@StartDate DATETIME
Based on the startdate passed in, I need to return all the rows of data for that week, with the week starting on Tuesday and ending the following Monday.
Only taking in a startdate, how do I set the start of the week to Tuesday and then have my query return data from that particular week's range?
I know NOTHING of date functions in SQL.
I did see you can set DATEFIRST as 2 (which I think is Tuesday), but I don't know how to take in the date and find that week. I assume I need two variables to set the beginning of the week date and the end of the week date and return data in that range?
Here's my query that currently only returns the current date passed in:
SELECT
PS.ProductionDate,
L.Name,
S.Name,
PS.PartNo,
--PM.DisplayName,
PS.LotNo,
WC.Name,
WC2.Name,
PSS.Name,
PS.Mixing,
PS.Glazing,
PS.StartTime,
PS.[Weight],
PS.Pallets
FROM PROD_ProductionSchedule PS
--LEFT OUTER JOIN PartMaster PM on PS.PartNo = PM.Name
LEFT OUTER JOIN Location L on PS.LocationId = L.Id
LEFT OUTER JOIN PROD_Shifts S on PS.ShiftId = S.Id
LEFT OUTER JOIN PROD_WorkCenters WC on PS.OvenId = WC.Id
LEFT OUTER JOIN PROD_WorkCenters WC2 on PS.LineId = WC2.Id
LEFT OUTER JOIN PROD_ProductionScheduleStatus PSS ON PS.ScheduleStatusId = PSS.Id
WHERE PS.ProductionDate=@StartDate
AND PS.LocationId = CASE WHEN @Location = -1 THEN PS.LocationId ELSE @Location END
AND PS.PartNo = CASE WHEN @PartNo = 'ALL' THEN PS.PartNo ELSE @PartNo END