简体   繁体   English

SSRS表达式-获取最近一个星期六的日期

[英]SSRS expression - get date of most recent saturday

I am trying to find an SSRS expression that will return the date of the most recent Saturday. 我正在尝试找到一个SSRS表达式,该表达式将返回最近的星期六的日期。 If the expression runs on a Saturday, I want it to display the Saturday of the previous week. 如果表达式在星期六运行,我希望它显示前一周的星期六。

I toyed around with an example I found online, and got it to produce the value I need it to produce. 我玩弄了一个在网上找到的示例,然后用它来产生所需的价值。 Listed below: 下面列出:

=IIf(Parameters!O_endDate.Value = nothing, DateAdd("d", -7-(WeekDay(Today(),7))+8, Today()), Parameters!O_endDate.Value)

I apologize for the stupid post. 我为这个愚蠢的帖子表示歉意。 I cannot understand why the above expression is producing the value I need, and I want to know if it will continue to work as I want it to work, when run on other days of the week in the future. 我不明白为什么上面的表达式会产生我所需要的值,并且我想知道当它在将来的一周中的其他日子运行时,是否会继续按我希望的方式工作。 Note: I ran this code on a Sunday. 注意:我在星期日运行此代码。

For your purposes, I don't think you need the IIF statement - this just returns the value of the parameter if it is not nothing. 为了您的目的,我认为您不需要IIF语句-如果不是空值,这只会返回参数的值。

This leaves the key operation as DateAdd("d", -7-(WeekDay(Today(),7))+8, Today()) . DateAdd("d", -7-(WeekDay(Today(),7))+8, Today())键操作DateAdd("d", -7-(WeekDay(Today(),7))+8, Today())DateAdd("d", -7-(WeekDay(Today(),7))+8, Today())

What this does is add -7-(WeekDay(Today(),7))+8 days to today - this can be simplified to 1-(WeekDay(Today(),7)) since -7+8=1. 这是在-7-(WeekDay(Today(),7))+8加上-7-(WeekDay(Today(),7))+8天-由于-7 + 8 = 1 1-(WeekDay(Today(),7))因此可以简化为1-(WeekDay(Today(),7))

Looking at the documentation here , the return_type appears to be invalid since the function is only defined for values of 1, 2 & 3 - if its working now then it may not continue to work. 查看此处的文档,return_type似乎无效,因为该函数仅针对值1、2和3进行了定义-如果该函数现在可以正常工作,则可能无法继续工作。

This will work all the time DateAdd("d", 1 - WeekDay(Today(), 1), Today()) if you want the most recent Sunday to be today when today is a Sunday or use DateAdd("d", WeekDay(Today(), 2)), Today()) if the most recent Sunday is last week when today is a Sunday. 如果您希望最近的星期日是今天,而今天是星期日,则可以一直使用DateAdd("d", WeekDay(Today(), 2)), Today()) DateAdd("d", 1 - WeekDay(Today(), 1), Today()) ,或者使用DateAdd("d", WeekDay(Today(), 2)), Today())如果最近的星期日是上周,而今天是星期日。

Note that in both cases this may produce the wrong answer at around midnight due to the 2 calls to Today() a few miliseconds apart. 请注意,在这两种情况下,由于两次调用Today()的时间相差几毫秒,所以这可能会在午夜左右产生错误的答案。 To be absolutely correct it should be: 绝对正确的是:

Declare @DateToCheck datetime=Today()
Select DateAdd('d', 1 - WeekDay(@DateToCheck, 1), @DateToCheck) 

This is a more generalised approach that should be closer to ANSI SQL. 这是一种更通用的方法,应该更接近于ANSI SQL。

See the SQL Fiddle for SQL Server 2008. 请参阅SQL Server 2008的SQL小提琴

Becuase the return value of DATEPART depends on a global database setting, the return value is adjusted for a known Saturday. 由于DATEPART的返回值取决于全局数据库设置,因此将返回值调整为一个已知的星期六。 By changing this to another day of the week you will get the last that day. 将其更改为一周的另一天,您将获得当天的最后一天。

您可以尝试以下操作: =DateAdd("d", -1 * (weekday(today()) mod 7 ), today())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM