简体   繁体   English

SSRS日期范围参数

[英]SSRS date range parameter

I have a report in SSRS 2016 that contains one SP which has a date field called startdate. 我在SSRS 2016中有一个报告,其中包含一个SP,该SP的日期字段称为startdate。 I'd like to create a parameter where the user can select between two ranges: startdate >='2017'or startdate < '2017'. 我想创建一个参数,用户可以在其中选择两个范围:startdate> ='2017'或startdate <'2017'。 This seems like it should be simple, but I can't see to find an easy way to do this. 这似乎应该很简单,但是我看不出找到一种简单的方法来做到这一点。

I've tried to create a parameter in SSRS where I chose "Specify Values" and manually added these values but I get an error that the "expression references the parameter "startdate" which does not exist in the Parameters collection. 我试图在SSRS中创建一个参数,在该参数中选择“指定值”并手动添加这些值,但是出现一个错误,即“表达式引用了参数”集合中不存在的参数“ startdate”。

I'm sure I can create this by creating a stored procedure with these dates, but that seems like more work than is needed. 我确信我可以通过使用这些日期创建一个存储过程来创建它,但这似乎比需要的工作还要多。

Does anyone know of a better way to do this? 有谁知道更好的方法吗?

If you are looking to have a parameter that has two options, 0 - Before 2017, and 1 - After 2017 then you should just create a Date parameter which has two options, Before 1/1/2017 in the label field with a 0 in the value field and After 1/1/17 in the label field with a 1 in the value field. 如果要查找具有两个选项的参数,0-2017年之前和1-2017年之后,则应仅创建一个具有两个选项的Date参数,在label字段中的1/1/2017之前,将0设置为值字段和标签字段中的1/1/17之后,值字段中为1。 Then in your report you just have to filter your data based upon the 1 or 0 value. 然后,在报表中,您只需要根据1或0值过滤数据即可。

For example: 例如:

DECLARE @DateFilter INT = 1

    IF ( @DateFilter = 0 )
        BEGIN
            SELECT * FROM dbo.MyTable t
            WHERE t.DateFilter < '1/1/17'
        END

    IF ( @DateFilter = 1 )
        BEGIN
            SELECT * FROM dbo.MyTable t
            WHERE t.DateFilter >='1/1/17' 
        END

I don't know why you would filter your data this way. 我不知道您为什么要这样过滤数据。 I recommend to use Larnu's suggestion that you have two parameters, a Start Date and an End Date. 我建议使用Larnu的建议,即您有两个参数,开始日期和结束日期。 Otherwise this could return a lot of rows for the second option as time goes on. 否则,随着时间的流逝,第二个选项可能会返回很多行。

Add a couple of parameters in SSRS say, FromDate and ToDate and make its data type as Date/Time. 在SSRS中添加几个参数,例如FromDate和ToDate,并将其数据类型设置为Date / Time。 You can specify the default values for these parameters using DateAdd functions and samples give below: 您可以使用DateAdd函数指定这些参数的默认值,示例如下:

Today: 今天:

=Today()

last week from today: 从今天开始的上周:

=DateAdd(DateInterval.Day, -7,Today())

You can add/reduce year, quarter month etc. in a similar way as shown below. 您可以按如下所示的类似方式添加/减少年份,季度月份等。 Just change the number to the required length 只需将数字更改为所需的长度

=DateAdd(DateInterval.Year,-1,Today())

=DateAdd(DateInterval.Quarter,-1,Today())

=DateAdd(DateInterval.Month,-1,Today())

=DateAdd(DateInterval.DayOfYear,-1,Today())

=DateAdd(DateInterval.WeekOfYear,-1,Today())

=DateAdd(DateInterval.WeekDay,-1,Today())

=DateAdd(DateInterval.Hour,-1,Today())

=DateAdd(DateInterval.Minute,-1,Today())

=DateAdd(DateInterval.Second,-1,Today())

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

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