0

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 
4

3 回答 3

1

获取本周的星期二和 7 天后:

select dateadd(dd, 3 - datepart(dw, getdate()), 
getdate()), dateadd(dd, 10 - datepart(dw, getdate()), getdate())
于 2013-04-18T21:38:18.633 回答
0

计算一周的开始和结束:

declare @StartOfWeek datetime
declare @EndOfWeek datetime

if DATEPART(weekday,@StartDate) = 3 --Tuesday
begin
    set @StartOfWeek = @StartDate
    set @EndOfWeek = dateadd(week,1,@StartDate)
end
else if DATEPART(weekday,@startdate) > 3
begin
    set @StartOfWeek = dateadd(DAY,-(DATEPART(weekday,@startdate)-3),@StartDate)
    set @EndOfWeek = dateadd(DAY,10-DATEPART(weekday,@startdate),@StartDate)
end
else
begin
    set @StartOfWeek = dateadd(DAY,-4-(DATEPART(weekday,@startdate)),@StartDate)
    set @EndOfWeek = dateadd(DAY,3-DATEPART(weekday,@startdate),@StartDate)
end

然后您可以在 Where 子句中使用这些:

WHERE PS.ProductionDate between @StartOfWeek and @EndOfWeek
于 2013-04-18T21:21:49.283 回答
0

首先,创建一个包含两列的日历表,其中包含任何给定日期的工作周的第一天和最后一天。然后你可以写一些非常清晰、简单的代码:

declare @FirstDayOfWorkingWeek date,
        @LastDayOfWorkingWeek date

select 
    @FirstDayOfWorkingWeek = FirstDayOfWorkingWeek, 
    @LastDayOfWorkingWeek = LastDayOfWorkingWeek
from
    dbo.Calendar
where
    [Date] = @StartDate
    
select *
from dbo.PROD_ProductionSchedule
where ProductionDate between @FirstDayOfWorkingWeek and @LastDayOfWorkingWeek

根据您存储日期的方式,请注意使用BETWEEN.

使用日历表几乎总是比使用函数计算日期更可取,因为您可以轻松读取和验证日期;你可以UPDATE让你的表处理常规逻辑的异常;您的代码通常更具可读性且更易于维护;JOIN如果您在表上而不是使用嵌套函数,性能会更好。

于 2013-04-18T21:58:42.417 回答