--这是使用SQL
我试过几次都失败了,
我有一个销售表,其订单日期采用以下格式“2014-05-15 00:00:00.000”
我想要一份报告,如果@today 小于周五,则显示上周的日期范围,如果@today 是周六或周日,则使用本周的日期范围
假设我只想查看 SalesOrder 和 OrderDate 字段
提前致谢,
院长
--这是使用SQL
我试过几次都失败了,
我有一个销售表,其订单日期采用以下格式“2014-05-15 00:00:00.000”
我想要一份报告,如果@today 小于周五,则显示上周的日期范围,如果@today 是周六或周日,则使用本周的日期范围
假设我只想查看 SalesOrder 和 OrderDate 字段
提前致谢,
院长
调整您的内心内容(在 SQL Server Management Studio 中运行此操作将显示带有正确输出的消息)。使用变量过滤您的SELECT
语句并删除PRINT
运算符:
DECLARE @ReportDate DATETIME
DECLARE @StartOfWeek DATETIME
DECLARE @DayOfWeek INT
SET @ReportDate = '2014-05-15 00:00:00.000' --This will be your variable in report builder
SET @DayOfWeek = DATEPART(dw,@ReportDate)
/*
Take away the day of the week from the report date to
get the beginning of that week.
*/
--Adjust the +1 to mark the start of your week (ie. +2 makes Monday the start)
SET @StartOfWeek = DATEADD(dd, (-(@DayOfWeek) + 1), @Reportdate)
--Now do stuff based on the report date (set the beginning of the week)
IF @DayOfWeek BETWEEN 2 AND 5 --Friday is day 6 (Sunday is first day in SQL and the BETWEEN clause is inclusive)
BEGIN
PRINT 'Monday to Thursday' --This line can be removed
/* Now minus 7 to get the beginning of the previous week */
SET @StartOfWeek = DATEADD(dd, -7, @StartOfWeek)
END
---------------------------------------------------
/*
This entire box is optional (can be removed) but just for demonstration purposes to
show that the date stuff works
*/
IF @DayOfWeek = 6 --Friday is day 6
BEGIN
PRINT 'Friday'
END
IF @DayOfWeek IN (7,1) --Saturday day 7, Sunday day 1
BEGIN
PRINT 'Saturday or Sunday'
END
---------------------------------------------------
--This is where your SELECT statement goes (instead of PRINT operators below)
PRINT 'StartOfWeek = ' + CAST(@StartOfWeek AS NVARCHAR(255))
PRINT 'EndOfWeek = ' + CAST(DATEADD(dd,7,@StartOfWeek) AS NVARCHAR(255))