使用本周/上周进行自动运行的报告

时间:2014-06-02 16:00:58

标签: sql date datetime daterangepicker sqlparameter

我已经在参数中设置了日期,如下所示,但我输了下一位...我想要的是;

如果@TodayDayOfWeek< 5(所以它是周一至周四)然后运行上周的日期(来自@Startoflastweek - @endoflastweek) 否则使用本周的日期范围

DECLARE @TodayDayOfWeek INT

DECLARE @EndOfThisWeek datetime

DECLARE @StartOfThisWeek datetime

DECLARE @EndOfPrevWeek DateTime

DECLARE @StartOfPrevWeek DateTime

SET @TodayDayOfWeek = datepart(dw, GetDate())

set @EndOfThisWeek =  DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 4)

set @StartOfThisWeek =  DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)

SET @EndOfPrevWeek = DATEADD(dd, -@TodayDayOfWeek, GetDate())

SET @StartOfPrevWeek = DATEADD(dd, -(@TodayDayOfWeek+6), GetDate())

1 个答案:

答案 0 :(得分:0)

假设您使用的是SQL Server,这是SProc

CASE WHEN @TodayDayOfWeek < 5 THEN
BEGIN
  -- YOUR LOGIC
END
ELSE
BEGIN 
  -- ELSE LOGIC
END