SSRS: Two cascading date parameters do not refresh - sql-server

I have an SSRS 2014 report with three parameters: #Period (text), #FromDate (date), and #ToDate (date). They work together by first selecting a value from the Period dropdown list (January, February, March etc...). Depending on what you period you choose, the #FromDate and #ToDate parameters change accordingly to reflect your choice. This works well, but the problem arises when you select a new period after having already selected one, as the date parameters do not refresh.
I have been looking at some suggestions and workarounds, but I have yet to find one that deals with two dependent date parameters. Any suggestions?

The Date parameters once generated cannot be changed. It does not have the cascading facility and according to Microsoft, it's by design (it's how they wanted it to behave):
Follow this link please

As already stated, parameters which have no available values/default values from a cascading query won't refresh their default values.
Workaround: Create a one row dataset which calculates your DateFrom and DateTo dates, each in a separate column and depending on your #Period parameter, and assign the dataset to the available and default values of both your parameters. Disadvantage: you won't be able to edit the values when running the report, since the fields are populated by a dataset.

This is working as intended, Microsoft doesn't want cascading date parameters to refresh if you change the parameter they are dependent on. However it's possible to get around it.
It requires two datasets for the two data parameters which return one row with the required data dependent on the #Period parameter e.g.
DECLARE #Dates as TABLE ([Period] INT, [Date] SMALLDATETIME)
INSERT INTO #Dates VALUES
(1,DATEADD(s, 86340, DATEADD(dd,-1,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0))) )
,(2,DATEADD(DAY , 7-DATEPART(WEEKDAY,GETDATE()),DATEADD(MINUTE,- 1,DATEADD(DAY,0,DATEADD(day,DATEDIFF(day,0,GETDATE())+1,0)))) )
,(3,DATEADD(MINUTE,- 1,DATEADD(DAY,0,DATEADD(day,DATEDIFF(day,0,GETDATE()),0))))
SELECT
[Period]
,[Date]
FROM
#Dates
WHERE
[Period] = #Period
Set the available values of the #Period parameter too match and set the default value of the data parameters to their matching datasets.
Now when you change the #Period parameter it forces the date datasets to be be rerun and your date parameters will default to the new result.

Related

Creating SQL views in VBA

I am trying to create a view on a table called petients in my database. The table has five columns. One of them is the column which I want to keep patient admitted date. It data type is datetime so I want to create a query that filters the data in this table based on current date. For example I want create a view that shows only details of petients who have been recorded on the current day.
Here is my code:
CREATE VIEW [dbo].[recent petients]
AS
SELECT petient_id, name, age, contact
FROM [petients]
WHERE [date] = 'date.Today'
I am getting an error saying that failed to convert date to string. Can you help me to solve it, or where is my code wrong?
Your code looks like SQL Server code. If so, I would recommend:
SELECT petient_id, name, age, contact
FROM [patients]
WHERE [date] = CONVERT(date, GETDATE());
As a note: This version is much better than DATEDIFF() because it allows the use of an index on patient([date]).
If the "date" column has a time component, you can use:
WHERE CONVERT(date, [date]) = CONVERT(date, GETDATE())
Note that this is also index-safe in SQL Server.
I'm assuming you are using Transact-SQL from Microsoft SQL Server, but you should specify the sql dialect you are using.
Since the datetime field type generally includes also a time, it is better to use the DATEDIFF function: https://learn.microsoft.com/it-it/sql/t-sql/functions/datediff-transact-sql?view=sql-server-ver15
In your case, to consider only the record where date=today, the difference in days must be zero:
--SQL QUERY
WHERE DATEDIFF(day, GETDATE(), [date]) = 0
day identifies the element you want to consider the difference. A list of names or abbreviations can be found in the link
GETDATE() returns now datetime
2nd and 3rd arguments are the dates you want to make the difference between

Date range based on Column Date

I am using the latest SQL Server. I have a table with a CreatedDate column. I need to write a Query that uses dates that are plus or minus 7 from the Date in CreatedDate. I have no clue how to go about this. My thought was this:
DECLARE #Date datetime
DECLARE #SevenBefore datetime
DECLARE #SevenAfter datetime
SET #Date = CreatedDate
SET #SevenBefore = DATEADD(day,-7,#Date)
SET #SevenAfter = DATEADD(day,7,#Date)
SELECT *
FROM <table>
WHERE <table> BETWEEN #SevenBefore AND #SevenAfter
The issue with this is that I cannot use "CreatedDate" as a SET #DATE because SQL gives an error "Invalid column name 'CreatedDate'"
Any help would be appreciated. I cannot list a date because every date in that column could be different.
Thanks
In this case, you need to stop thinking as a programmer would, and start thinking as a Database programmer would.
Lets work only with this central part of your query:
SELECT *
FROM <table>
WHERE <table> BETWEEN #SevenBefore AND #SevenAfter
Now, you say that the CreatedDate is a column in a table. For this example, I will assume that the CreatedDate is in a table other than the one in your example above. For this purpose, I will give two fake names to the tables. The table with the CreatedDate, I will call tblCreated, and the one from the query above I will call tblData.
Looking above, it's pretty obvious that you can't compare an entire table row to a date. There must be a field in that table that contains a date/time value. I will call this column TargetDate.
Given these assumptions, your query would look something like:
SELECT *
FROM tblCreated tc
INNER JOIN tblData td
ON td.TargetDate BETWEEN DATEADD(day, -7, tc.CreatedDate) and DATEADD(day, 7, tc.CreatedDate)
Looking at this, it is clear that you still need some other associations between the tables. Do you only want all data rows per customer based on the Created date, or perhaps only want Creations where some work was done on them as shown in the Data records, or ??. Without a fuller specification, we can't help with that, though.

Change value of SSRS report parameter based on the input of another parameter

I have a SSRS 2005 report with three parameters: #FromDate, #ToDate and #Period. #FromDate and #ToDate default to the first day and the last day of last month, respectively.
The #Period parameter is a multi-value parameter. So if I choose for instance "Current quarter", I want the #FromDate and #ToDate to change to the corresponding values based on the input in #Period.
My problem is not getting the correct datetime expressions, but rather how to make the parameters interact with each other as desired.
To achieve your requirement, we need use cascading parameters in SSRS.
In your scenario, you need create the parameter #FromDate that get values from a dataset which select FromDate based on the #Period parameter. Then create parameter #ToDate that get values from a dataset which select ToDate based on the #FromDate parameter.
The following document is for your reference:
https://technet.microsoft.com/en-us/library/aa337498(v=sql.105).aspx

Comparing dates with current date in Sql server

I have a table which has list of some events with dates. I am trying to write a stored procedure that will return only the upcoming events.
I have written the following query in the stored procedure:
SELECT *
FROM Events
WHERE tDate >= (select CAST(GETDATE() as DATE))
But this is not returning correct result. This is also showing results that have dates less than current date. How to write a query that will return all the events that have date equal or greater than today's date.
Edit: Dates that have been entered on the table have the format yyyy/dd/mm and getdate() returns date in the format yyyy/mm/dd. I think this is causing the problem. Dates that have been entered into the table has been taken using jquery date picker. Any solution to this problem?
Not sure why you have an additional select
SELECT *
FROM Events
WHERE tDate >= CAST(GETDATE() as DATE)
your DATE data is incorrectly stored within Sql Server. When your application passes the string '2015-09-04' and you save that your date column, it is saved as 4th Sept 2015 and not 9th April 2015. Hence your query returns such rows as they are greater than GETDATE().
Example
DECLARE #D VARCHAR(10) = '2015-09-04'
SELECT CONVERT(VARCHAR(20),CONVERT(DATE,#D),109)
you need to fix your data and then use a CONVERT with style when saving dates in your table from application, using something like this. CONVERT(DATE, '20150409',112)
DECLARE #D VARCHAR(10) = '20150409'
SELECT CONVERT(VARCHAR(20),CONVERT(DATE,#D,112),109)
Refer these threads for more info:
Impossible to store certain datetime formats in SQL Server
Cast and Convert

How to Get rows that are added 'x' minutes before in sqlserver?

I want to get all rows that have being added 'x' minutes before.
SELECT [PromoCodeID]
,[CustomerID]
,[DiscountAmount]
,[AddedBy]
,[AddedDate]
,[ModifiedBy]
,[ModifiedDate]
FROM [tbl_PromoCodesNewCustomer]
Where....
Eg: Records added 30min before from todays date time
NOTE: Record Added date is added in the field 'AddedDate' which has DATETIME datatype.
You can use this:
SELECT [PromoCodeID]
,[CustomerID]
,[DiscountAmount]
,[AddedBy]
,[AddedDate] AS added
,[ModifiedBy]
,[ModifiedDate]
FROM [tbl_PromoCodesNewCustomer]
WHERE DATEADD(minute,x,added) > GETDATE()
Where "x" in DATEADD is the number of minutes you want.
i suggest a light variation on FirstHorizon answer:
SELECT [PromoCodeID]
,[CustomerID]
,[DiscountAmount]
,[AddedBy]
,[AddedDate] AS added
,[ModifiedBy]
,[ModifiedDate]
FROM [tbl_PromoCodesNewCustomer]
WHERE AddedDate < DATEADD(minute * -1,x,getdate())
the change may look minor but depending on the number of involved rows, indexes and some other factor evaluated by the query optimizer this query may perform way better because there is no calc to make on the data.
here is an article that explain the reason (look at the second paragraph, 'Using Functions in Comparisons within the ON or WHERE Clause').
EDIT:
I'd advise you to populate the addedDate field with the UTC date and time, otherwise you could have problems with data managed among different servers \ time zones or on time change days.
So, if we consider this, the where clause will be:
WHERE DATEDIFF(mi,addedDate,GETUTCDATE)<30

Resources