您好我已经将电子表格转换为SSRS报告,该报告在月20日运行,我需要通过自定义表达式或数据集将当月的20日作为默认值(开始日期)。如果20日是星期六,则默认为星期五19日。如果20日在星期日默认为星期五18日。我该怎么做以及最好的方法是什么?
答案 0 :(得分:2)
这个怎么样:
DECLARE @daytwenty date;
SET @daytwenty = DATEADD(mm, DATEDIFF(mm, 0, getdate()) ,19) --Get Twentieth Day of current month
SELECT
CASE DATEPART(DW,@daytwenty)
WHEN 1 THEN DATEADD(dd, -2, @daytwenty) --When the twentieth day is a Sunday, take two days off
WHEN 7 THEN DATEADD(dd, -1, @daytwenty) --When the twentieth day is a Saturday, take one day off
ELSE @daytwenty --Else the twentieth day is a weekday already
END
答案 1 :(得分:0)
您应该使用Datetime for 2008版本。通过写作
DECLARE @LatestDate date;
在以前版本的Sql Server中出现编译错误。
Column, parameter, or variable #1: Cannot find data type date.
以下是代码 Sql server 2008
DECLARE @LatestDate datetime;
SET @LatestDate = DATEADD(mm, DATEDIFF(mm, 0, getdate()) ,19) --Get Twentieth Day of current month
SELECT
CASE DATEPART(DW,@LatestDate)
WHEN 1 THEN DATEADD(dd, -2, @LatestDate) --When the twentieth day is a Sunday, take two days off
WHEN 7 THEN DATEADD(dd, -1, @LatestDate) --When the twentieth day is a Saturday, take one day off
ELSE @LatestDate --Else the twentieth day is a weekday already
END
答案 2 :(得分:-1)
我希望这就是你想要的,
select case datename(dw, getdate())
when 'Monday' then 'Friday'
when 'Tuesday' then 'Monday'
when 'Wednesday' then 'Tuesday'
when 'Thursday' then 'Wednesday'
when 'Friday' then 'Thursday'
when 'Saturday' then 'Friday'
when 'Sunday' then 'Friday'
end