简体   繁体   English

SSRS表达式从DateAdd()获取年份

[英]SSRS expression to get the year from the DateAdd()

I am trying to get the year part from the DateAdd function. 我试图从DateAdd函数中获取年份部分。 I am going wrong some where. 我在哪里错了。 I tried many methods but still hit with he error 我尝试了很多方法,但仍然遇到了错误

Datepart("yyyy", DateAdd("M",-2,Now)) This does not work. Datepart(“yyyy”,DateAdd(“M”, - 2,Now))这不起作用。 Can you help 你能帮我吗

It looks like you're trying to get the year for the day two months ago: 看起来你想要在两个月前获得当天的一年:

=DatePart(DateInterval.Year, DateAdd(DateInterval.Month, -2, Now()))

Edit after comment 评论后编辑

In your expression in the comment you're getting an error trying to add a string and an integer together. 在注释中的表达式中,您尝试将字符串和整数一起添加时出错。

So it looks like the actual requirement is to get a string like {Month name}-{year} . 所以看起来实际的要求是获得像{Month name}-{year}这样的字符串。

You can do this by applying a Format expression to the date, either in the textbox directly: 您可以通过将格式表达式应用于日期来直接在文本框中执行此操作:

=Format(DateAdd(DateInterval.Month, -2, Now()), "MMMM-yyyy")

Or add MMMM-yyyy as the Format property to a textbox with the very first expression: 或者使用第一个表达式将MMMM-yyyy作为Format属性添加到文本框中:

在此输入图像描述

Either way, for today you get: 无论哪种方式,今天你得到:

在此输入图像描述

  • MonthName(Month(DateAdd("M",-2,Now))) + " - " + DatePart(DateInterval.Year, DateAdd(DateInterval.Month, -2, Now)) MonthName(月(DateAdd(“M”, - 2,现在)))+“ - ”+ DatePart(DateInterval.Year,DateAdd(DateInterval.Month,-2,Now))

This also workes when we replace '+' with '&' symbol... 当我们用'&'符号替换'+'时,这也有效...

  • MonthName(Month(DateAdd("M",-2,Now))) & " - " & DatePart(DateInterval.Year, DateAdd(DateInterval.Month, -2, Now)) MonthName(Month(DateAdd(“M”, - 2,Now)))&“ - ”&DatePart(DateInterval.Year,DateAdd(DateInterval.Month,-2,Now))

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

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