I have an SSRS report which uses a @Year parameter, which is chosen by the user at run-time.
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. 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).
The SQL at the moment is (miminum required):
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'?
EDIT : Please note that the actual format for the year is (eg) 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.
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.
Add a computed @TermStart parameter to the dataset with the following formula:
=DateSerial(2000 + CInt(Split(Parameters!Year.Value,"/")(0)),8,31)
(So long as you aren't expecting any dates prior to 2000 of course)
Then you can use the @Year and @TermStart parameters in the query like so:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.