SQL Server Reporting Services Subscription with dynamic parameters - sql-server-2005

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')

Related

SSRS with a parameter #startdate

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

Using REPLACE to change datetime with SET

I haven't used this website before and am very new to SQL so please excuse any errors or if this question has been raised incorrectly - I have been asked to do something that is well outside of my remit and skill level!
I have many rows of data with a particular column - COLUMN - which has a DATETIME datatype, in a particular table - let's call it _data, and the data within it looks like this: 2019-11-12 17:00:00.000
I want to replace the YY-MM-DD portion of all of the columns in this table with today's date, 2019-11-28.
I am trying this code:
update DATABASE.dbo._data
set COLUMN = REPLACE(COLUMN,'2019-11-13','2019-11-28')
Which executes and informs me that every row in DATABASE has changed, but when I look at the data with a SELECT statement, it is the same as it was before. Why is this, do I need to do something different because the datatype is DATETIME? Google did not elucidate me and neither did w3schools or the other questions I searched for on here, but that's probably because I have no idea what I am doing.
I am using SQL SERVER 2012 on a Windows Server 2012 R2 box and I am running this code with SQL Server Management Studio v18.4.
DATETIMEFROMPARTS is your best friend here.
DECALRE #now DATETIME = GETDATE();
UPDATE DATABASE.dbo._data
SET COLUMN = DATETIMEFROMPARTS(
YEAR(#now),
MONTH(#now),
DAY(#now),
DATEPART(hour, COLUMN),
DATEPART(minute, COLUMN),
DATEPART(second, COLUMN),
DATEPART(millisecond, COLUMN));
This seems like a really add logic, but seems like the "easiest" method would be to use DATEADD and DATEDIFF:
UPDATE dbo.YourTable
SET DateTimeColumn = DATEADD(DAY, DATEDIFF(DAY, DateTimeColumn, GETDATE()), DateTimeColumn);

Fetch record from database using stored procedure with month and year condition

I am trying to fetch record from database by getting and parsing year and month from date parameter. Here is the code in Where clause in my stored procedure:
(MONTH(ai.InstallmentDueDate)) = (MONTH(#DueDate))
AND (YEAR(ai.InstallmentDueDate)) = (YEAR(ai.InstallmentDueDate))
The month clause works perfectly, but it returns all the years data of that month. Please tell me what to do. So that it returns data of that specific year.
Is this what you required?
(MONTH(ai.InstallmentDueDate)) = (MONTH(#DueDate))
AND (YEAR(ai.InstallmentDueDate)) = (YEAR(#DueDate))
But I think simply passing the parameter without applying any function can achieve what you want.
ai.InstallmentDueDate = #DueDate
(Be careful when you use functions in where clause. It's a performance hit)
You are not using the parameter in the year, that's why it's not working.
Check your original post, you're comparing the ai.InstallmentDueDate to itself. You need to compare it against the year of the passed parameter
(MONTH(ai.InstallmentDueDate)) = (MONTH(#DueDate))
AND (YEAR(ai.InstallmentDueDate)) = (YEAR(#DueDate))
WHERE ai.InstallmentDueDate >= DATEADD(MONTH, DATEDIFF(MONTH, 0, #DueDate), 0)
AND ai.InstallmentDueDate < DATEADD(MONTH, DATEDIFF(MONTH, 0, #DueDate) + 1, 0)

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.

Error in Updating a table using datetime as parameter in Stored procedure

Here is my query:
UPDATE Mst_Attendance
SET FNLogged=#FNLogged,
ANLogged=#ANLogged,LogTime=#LogTime,LogOuttime=#LogOuttime
WHERE EmployeeId=#Employee_id AND Atdate = CONVERT(VARCHAR(10), #AtDate, 101) AS [MM/DD/YYYY]
-- Convert(Datetime,#AtDate)
SELECT * FROM Mst_Attendance where Atdate=#AtDate and EmployeeId=#Employee_id
Error occured near AS
Just remove the AS [MM/DD/YY] snippet. You don't need it, and it's not valid inside a WHERE clause.
And what in the world are you doing storing dates as strings in your database? That's just a bad idea. Are you trying to truncate the time portion?
AS in that context is used to give an alias to a column or table; there is no sense in an AS here, since that isn't a select.
You have already specified a format via CONVERT(VARCHAR(10), #AtDate, 101), but this also seems odd; dates are not strings. If you are matching on a datetime - keep everything as a datetime.
If you are actually trying to remove the time portion (leaving just a date), either a: don't send the time (cut it at the caller), or b: do something like:
set #date = cast(floor(cast(#date as float)) as datetime)