简体   繁体   English

SSRS:在查询中嵌入@Year参数

[英]SSRS : Embed a @Year parameter in a query

I have an SSRS report which uses a @Year parameter, which is chosen by the user at run-time. 我有一个使用@Year参数的SSRS报告,该参数由用户在运行时选择。

Fine, so far, but the SQL in the Data set Properties section contains a hard-coded date of '2010-08-31' , but, the year part of it needs to be the same as the @Year parameter which the user chooses. 到目前为止,还不错,但是“数据集属性”部分中的SQL包含一个硬编码的日期'2010-08-31',但是,其年份部分必须与用户选择的@Year参数相同。 In other words, if you run the report in the year 2010 the results will be correct, but not if you run it now (in 2014). 换句话说,如果您在2010年运行报表,结果将是正确的,但如果现在(2014年)运行,则结果将不正确。

The SQL at the moment is (miminum required): 目前的SQL是(最低要求):

SELECT  DateDiff(Year, birth_dt, '2010-08-31')
--Date of Start of Academic Term
FROM    table99
WHERE  acad_period = @Year

...so my question is, what is the correct syntax for substituting the @Year value in place of '2010'? ...所以我的问题是,用@Year值代替'2010'的正确语法是什么?

EDIT : Please note that the actual format for the year is (eg) 12/13, 13/14 编辑:请注意,该年份的实际格式为(例如)12 / 13、13 / 14

you can replace the line 你可以更换线

SELECT DateDiff(Year, birth_dt, '2010-08-31')

with

SELECT DateDiff(Year, birth_dt, @Year+'-08-31')

To do the same with current date 对当前日期执行相同操作

SELECT DateDiff(Year, birth_dt, DATEPART(yyyy, getdate())+'-' + DATEPART(mm, getdate()) +'-'+DATEPART(dd, getdate()))

based on your clarification in comment, if you pass in '12/13' your query would be something like this. 根据您在评论中的澄清,如果您输入“ 12/13”,则查询将是这样的。

SELECT DATEDIFF(Year, birth_dt, '20'+LEFT(@Year,2) + '-08-31')
FROM    table99
WHERE  acad_period = @Year

Since your year parameter isn't a simple year value but is instead a string like "13/14" presumably meaning the 2013/2014 school year, I would definitely handle parsing it outside of the query. 由于您的year参数不是简单的year值,而是类似“ 13/14”的字符串,大概意味着2013/2014学年,因此,我肯定会在查询之外进行解析。

Add a computed @TermStart parameter to the dataset with the following formula: 使用以下公式将计算的@TermStart参数添加到数据集中:

=DateSerial(2000 + CInt(Split(Parameters!Year.Value,"/")(0)),8,31)

(So long as you aren't expecting any dates prior to 2000 of course) (只要您不希望当然有2000年之前的任何日期)

Then you can use the @Year and @TermStart parameters in the query like so: 然后,您可以在查询中使用@Year和@TermStart参数,如下所示:

SELECT  DateDiff(Year, birth_dt, @TermStart)
--Date of Start of Academic Term
FROM    table99
WHERE  acad_period = @Year

But as I mentioned in a comment above, that is not the correct way to calculate age. 但是,正如我在上面的评论中提到的那样,这不是计算年龄的正确方法。 There are several ways to do that. 有几种方法可以做到这一点。 My favorite is this: 我最喜欢的是:

SELECT  datediff(hour,birth_dt,@TermStart)/8766
--Date of Start of Academic Term
FROM    table99
WHERE  acad_period = @Year

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

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