Get all dates greater than current date in SSRS - sql-server

I have two parameters in my Report - Begin and End Dates which are of Date/Time type. When I select the Begin Date from the Calendar, how do I automatically show dates equal to or greater than Begin Date for the End Date calendar? I would like the have the user the ability to select only End dates after Begin date.

This could be a potential workaround going off the comment I made to your question. You would need three datetime parameters:
-#1 StartDate
-#2 EndDate
-#3 EndDate2 - make this hidden
The StartDate and EndDate (#1 and #2) are for users to select. For the report, we will use StartDate (#1) and EndDate2 (#3) to filter.
You can set the default for StartDate/EndDate however you would like. For EndDate2, if the user selects a date prior to the start date, default the end date that gets passed to the report. We set this by adjusting the default value for EndDate2 parameter with the IIF function to compare the user input dates.
Add this to default value for EndDate2 parameter:
=IIF(Parameters!EndDate.Value < Parameters!StartDate.Value, Parameters!StartDate.Value, Parameters!EndDate.Value )

Related

SSRS Parameter Not Dynamically Updating - Last Day of Month

Report has two parameters
DateFrom
DateTo
DateTo set to "Always Refresh"
I have the following expression for DateTo - Default Values - so that it will dynamically default to the last of the month set by DateFrom
=DateSerial(Year(Parameters!DateFrom.Value), Month(Parameters!DateFrom.Value), "1").AddMonths(1).AddSeconds(-1)
However, the field doesn't update.
It shows the correct last date of month when DateFrom is selected the first time.
If DateFrom is set to 15/02/2019 then DateTo will show "28/02/2019 23:59:59".
But if you subsequently change DateFrom to a different month, DateTo doesn't update - still shows "28/02/2019 23:59:59".
I have just added these Parameter properties
1) data type -> date/time and allow nulls
2) available values -> specify values -> label and value as
=DateSerial(Year(Parameters!Datefrom.Value), Month(Parameters!Datefrom.Value), "1").AddMonths(1).AddSeconds(-1)
3) Advanced=always refresh. and checked notify me box.
This works the moment i change param 1 ('Datefrom') i get new dateto as last date from month.
Are your settings matching this?

SSRS date/time parameter

I have a ssrs report that has a date/time parameter that allows the user to select the date when running report. how to I get it to exclude the time part of the date field.
currently the field in the db is a date/time field so when I run query
select count(*) from table where date <= #dateparameter
it is not including records where the time part of field is greater than 00.00.00
how can I ignore the time part so all records are returned for that date
The simplest (and probably best performance) solution would be to add a day to the date passed by the user amd change the <= to <:
select count(*) from table where date < DATEADD(DAY, 1, #dateparameter)

SSRS: Two cascading date parameters do not refresh

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.

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

Resources