SSRS with a parameter #startdate - sql

I currently have an SSRS report that runs daily with the below SQL code as it should. But, when replication goes down and we miss a day, our Court people would like to be able to manually run a missed date by entering a date #1.
I need some help with setting up the parameter #StartDate, that will run the code below.
I have this SQL:
Select *
from Court
WHERE
case_filed_dt =
CASE WHEN datepart(weekday,getdate())= 2 THEN
datediff(day,3,getdate())
ELSE
datediff(day,1,getdate())
END
Order by court asc
Simple case statement that looks at the date the report is run, if it runs on Monday's, it get Friday's data otherwise previous day's data.
I would like to add a parameter #startdate for my "case_filed_dt" field, to run manually, in case a report is missed.
Example:
If I run for #startdate = '06-06-2022' it will do as my case statement code does, and get data for '06-03-2022'. If I run for #startdate ='06-07-2022', data is for 6-06-2022'.
Thanks,
jer

I would keep this simple.
Change your existing dataset query to accept a parameter (pStartDate) like this..
Select *
from Court
WHERE
case_filed_dt =
CASE WHEN datepart(weekday, #pStartDate)= 2 THEN
datediff(day,3, #pStartDate)
ELSE
datediff(day,1, #pStartDate)
END
Order by court asc
Then in your report, set the parameter's default value to be an expression
=Today()
Then if the report is run as normal with no parameters passed, it will use Today() as the start date or if the report is run manually, any date can be selected by the user.

a few versions on the fiddle, which should get you started, this one using a variable to simulate the effects of the Case statement.
Change the date and see what happens
Declare #DateNow datetime = '2022-06-03'
SELECT #DateNow as YourDate, DATENAME(WEEKDAY, #DateNow) AS DayNow, '',
CASE
WHEN DATENAME(WEEKDAY, #DateNow) = 'Monday' THEN
DateAdd(day,-3,#DateNow)
ELSE
DateAdd(day,-1,#DateNow)
END AS ReportDate
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=b3ae703d3be9ace930822f7e20230018

Related

Using a Parameterized column in a Where Clause using Case

I am building a report in SSRS where I need to be able to let our users filter on different date fields. They need to be able to select the date filter and then the start and end dates within that filter.
This is a simplified Example of what I am trying to achieve:
SELECT Project.ProjectID, Project.Name, Project.Update_Date, Project.Due_Date, Project.Start_Date, Project.Review_Date
FROM Project
WHERE #datetypefiler BETWEEN #startdate AND #enddate
Where the #datetypefiler will essentially equal the field the user wishes to filter on (i.e. Project.Update_Date or Project.Due_Date etc)
On SSRS i built parameters for #datetypefiler which could equal the various date fields and I set up the start date and end date filter as expected.
Unfortunately it does not seem as though I can parameterize the #datetypefiler in this way? I then attempted to use the Case keyword as so:
SELECT Project.ProjectID, Project.Name, Project.Update_Date, Project.Due_Date, Project.Start_Date, Project.Review_Date
FROM Project
WHERE CASE
when #datetypefiler='Project.Update_Date' then Project.Update_Date BETWEEN #startdate AND #enddate
when #datetypefiler='Project.Due_Date' then Project.Due_Date BETWEEN #startdate AND #enddate
This isnt working for me either, I figure that instead of taking shots in the dark I should ask what the correct method would look like. Any advice on this would be greatly appreciated,
You could try something like this:
SELECT *
FROM Project
WHERE (#datetypefiler='Project.Update_Date'
AND Project.Update_Date BETWEEN #startdate AND #enddate)
OR (#datetypefiler='Project.Due_Date'
AND Project.Due_Date BETWEEN #startdate AND #enddate)
The OR will only evaluate the full statement if the #datetypefiler is matched.
Or you could try this
SELECT Project.ProjectID, Project.Name, Project.Update_Date, Project.Due_Date,
Project.Start_Date, Project.Review_Date
FROM Project
where
(CASE
when #datetypefiler = 'Project.Update_Date' then Project.Update_Date
when #datetypefiler= 'Project.Due_Date' then Project.Due_Date
END) BETWEEN #startdate AND #enddate
While both of other two answers are going to work just fine, they won't make use of index. I'd recommend to have two separate SELECT statements with two different queries based on your criteria, this way you can index your table both on Update_Date and Due_Date and actually use them to speed your queries up.

Stored Proc run manually differs from run via job

I have a stored proc which when run manually, produces expected results. When run by the Job Agent, I get very different results and I cannot work out why. The issue relates to the following
declare #date as date
set #date=case datepart(dw,getdate())
when 1 then GETDATE()-3 --Monday so use Friday
when 7 then GETDATE()-2 --Sunday so use Friday
else GETDATE()-1 end
print #date
It appears that when run as a job, #date is being set to GETDATE()-1 regardless on a Monday. But appears to be set to GETDATE()-2 correctly on a Sunday....
What have I cocked up?
Have a look at SET DATEFIRST https://msdn.microsoft.com/en-us/library/ms181598.aspx
One possible reason could be that the job login has a different setting than your own login.

Using a Lookup function in a Parameter Default

Goal: To provide dynamic date calculations for SSRS Parameters to be used in scheduling.
I have a report containing two date parameters, DateRangeBegin and DateRangeEnd. The problem we are running into is that users want to schedule this report for a date range like "Month to Date", "Year to Date", "Last Week", etc. When a user goes to schedule the report, they can only provide static dates for these two parameters.
The idea I had was to create a dataset that will calculate these values and be referenced in another parameter called DynamicDate. The user would then select "Yesterday", from the DynamicDate parameter, and the DateRangeBegin and DateRangeEnd parameters would get updated with the calculated values from the dataset.
The dataset would be something like this:
Select
2 as DateCalcId,
'Yesterday' as DateCalcDescription,
CONVERT(VARCHAR, DATEADD(DAY,-1,GETDATE()), 101) as DateCalcBegin,
CONVERT(VARCHAR, DATEADD(DAY,-1,GETDATE()), 101) as DateCalcEnd
UNION ALL
Select
1 as DateCalcId,
'Today' as DateCalcDescription,
CONVERT(VARCHAR, DATEADD(DAY,0,GETDATE()), 101) as DateCalcBegin,
CONVERT(VARCHAR, DATEADD(DAY,0,GETDATE()), 101) as DateCalcEnd
UNION ALL
Select
3 as DateCalcId,
'Month to Date' as DateCalcDescription,
CONVERT(VARCHAR,(CONVERT(datetime, CONVERT(VARCHAR, Year(GetDate())) + '-' + Convert(Varchar,Month(GetDate())) + '-01')), 101) as DateCalcBegin,
CONVERT(VARCHAR, DATEADD(DAY,0,GETDATE()), 101) as DateCalcEnd
order by
DateCalcId
I think the function I'm wanting to use is the Lookup() function in the date parameters for their default values, but I'm having a little trouble with the syntax. So far I have:
=Lookup(Parameters!DynamicDate.Value, Fields!DateCalcId.Value, Fields!DateCalcBegin.Value, "CalculatedDates")
But I'm getting this error:
A Value expression used for the report parameter 'DateRangeBegin'
refers to a field. Fields cannot be used in report parameter
expressions.
Does anyone know how to get this working? Or is there another way people have done this?
So I think I've come up with a pretty good solution that fits my requirements.
I really like the idea of having these dynamic date range pairs being calculated in a SQL Dataset because they can be reused across multiple reports, don't require a custom dll to be created, and the pair can be calculated in a single record.
So the solution is to have a single stored procedure that takes a nullable parameter, where passing in null returns the entire set, but passing in the DateCalcId will return a single record that can then be attributed to the range beginning and end parameters.
So the entire dataset would populate the Available Values for the first parameter (#DynamicDate), where the Value field is the "DateCalcId" column, and the Label field is the "DateCalcDescription" column.
Then the date range parameters default values would be tied to the single result dataset using the #DynamicDate value as the input to the stored procedure.
This allows the user to select the Dynamic Date and the date range parameters are defaulted to the calculated dates, and the user is able to overwrite these dates if they desire.
If the dates should always be calculated based on the value selected, then the date parameter Available values can be set to the single result dataset as well.
Here is the stored procedure:
Create PROCEDURE [CNF].[RptCalculatedDateRanges]
#DynamicDateId int = null
AS
BEGIN
SET NOCOUNT ON;
Declare #Today datetime = convert(varchar,getdate(),101)
Select
*
from
(
Select
2 as DynamicDateId,
'Yesterday' as DynamicDateDescription,
DATEADD(DAY,-1,#Today) as DynamicDateBegin,
DATEADD(DAY,-1,#Today) as DynamicDateEnd
UNION ALL
Select
1 as DynamicDateId,
'Today' as DynamicDateDescription,
#Today as DynamicDateBegin,
#Today as DynamicDateEnd
UNION ALL
Select
3 as DynamicDateId,
'Month to Date' as DynamicDateDescription,
CONVERT(VARCHAR,(CONVERT(datetime, CONVERT(VARCHAR, Year(#Today)) + '-' + Convert(Varchar,Month(#Today)) + '-01')), 101) as DynamicDateBegin,
#Today as DynamicDateEnd
) D
where
#DynamicDateId = D.DynamicDateId or
#DynamicDateId is null
order by
DynamicDateId
END
You don't to use a dataset, you can just use the Default expression to calculate the parameter date value using VBA. For example, DateRangeEnd for the following dates would be:
Yesterday:
=DateAdd(DateInterval.Day, -1, Today)
End of Last Month:
=DateAdd(DateInterval.Day, -1, DateAdd(DateInterval.Day, 1-Day(Today), Today))
So you have a SWITCH statement based on your DynamicDate parameter:
=Switch(Parameters!DynamicDate.Value = 1, Today, Parameters!DynamicDate.Value = 2, DateAdd(DateInterval.Day, -1, Today))
and so on for all your date range choices.

SQL Server Reporting Services Subscription with dynamic parameters

I have a custom report in SQL Server Reporting Services.
This report has a few parameters, like: Requested date start, Requested data to, donor.
I would like to setup a scheduled email.
However I would like to assign the "Requested date start" value to = (current datetime-1 month)
and assign the "Requested date end" value to = current datetime
Any tips on how to do this?
This is quite simple, see my answer here:
SQL Server: calculating date ranges
What you have to end up doing is creating these calculations as a dataset and then use the "Use Default" checkbox in the subscription itself:
Write a stored procedure and get the following from the sql in the stored proc:
select #FileName as FILENAME, #PATH as PATH, 'EXCEL' as RENDER_FORMAT , 'Overwrite' as WRITEMODE,
'True' as FILEEXTN , 'null' as USERNAME, 'null' as PASSWORD
Call the proc from the subscription
Anil Madan
I had a similar issue. I created the subscription with an initial set of parameters and set the run time. Then I created a sproc and a job to run the sproc before the schedule run time of the subscription. Your date math will be different from mine depending on your needs.
I then copied the parameter settings to notepad, found the parts I needed to change and put them on their own lines. Then put them in the sproc as below.
UPDATE ReportServer.dbo.Subscriptions
SET Parameters =
(SELECT '<ParameterValues><ParameterValue><Name>Summary</Name><Value>PODETAIL</Value></ParameterValue><ParameterValue><Name>requisitioner_erp_user_id</Name><Value>51</Value></ParameterValue><ParameterValue><Name>requisitioner_erp_user_id</Name><Value>125</Value></ParameterValue><ParameterValue><Name>date_range_type</Name><Value>3</Value></ParameterValue><ParameterValue><Name>po_date_end</Name><Value>'
+
(SELECT FORMAT(DATEADD(wk, DATEDIFF(wk, 1, GETDATE()), -1), 'MM/dd/yyyy') AS po_date_end)
+
'</Value></ParameterValue><ParameterValue><Name>po_date_start</Name><Value>'
+
(SELECT FORMAT(DATEADD(wk, DATEDIFF(wk, 0, GETDATE()) - 1, 0), 'MM/dd/yyyy') AS po_date_start)
+
'</Value></ParameterValue></ParameterValues>'
)
WHERE ReportServer.dbo.Subscriptions.SubscriptionId IN ('B6645FD3-DE27-4551-8331-C0135305CC79')

SQL Start date is current day then end date is 1 month ago even if its not from the first to the last of the month

Ok I am trying to write a query that says get the current date and make it the start date. Then I want to go a month back from that current date for the EndDate. Is it possible to do this? Like if it was 9-15-2010 as the start date can I go a month back from that to 8-15-2010 or is this no possible....what would you do for like 9-20-2010 as the start date since every month has a different amount of days in it? Otherwise if this is not possible how else could I do this? The report will always be run on the 25th of the month so any ideas? I need to go from the 25th back a month....I can get some duplicate records between months if needed but less is obviously better
Right now I am using this:
DECLARE #StartDate DATETIME,
#EndDate DATETIME;
SET #StartDate = DATEADD(m,-1,GETDATE());
SET #EndDate = DATEADD(m, 1, #StartDate);
Does this work?
Also, how would I then say my AuditInsertTimestamp is between #Start adn #EndDate?
Currently I have this:
AND cvn.[AuditInsertTimestamp] BETWEEN #StartDate AND #EndDate ;
This is still giving me dates like 7-26-2010 though....
Thanks!
That should work. Did you try it?
If it doesn't work (and there are only 12 test cases to check if you don't trust the documentation) then you can re-build the date from the date parts.
Here's the problem. It should be like this:
cvn.[Subject] = 'Field Changed (Plate Type)'
AND (
cvn.[Note] LIKE 'Old Type: IRP%New Type: BASE PLATE%'
OR cvn.[Note] LIKE 'Old Type: Base Plate%New Type: IRP%'
)
AND cvn.AuditInsertTimestamp BETWEEN GETDATE() AND DATEADD(MONTH, -1, GETDATE())
AND takes precidence over OR, so you were picking up anything with Old Type:IRP or in the correct date range (with Old Type: Base Plate)
Based on your comment:
Well this is being used to select
records. So if I run it on the 25th I
need 30 days back then my field
AuditInsertTimestamp needs to be
between these 2 dates.
I think you need to do something like this:
SELECT * FROM Table
WHERE AuditInsertTimestamp BETWEEN GETDATE() AND DATEADD(MONTH, -1, GETDATE())