SSRS parameters based on selection? - sql

I hope someone could help with this?
I am building a report based on a stored procedure that has the following clause in it
where accountingperiod = #Accountingperiod or createddate between #Startdate and #Enddate
#Accountingperiod will be in the following Format 'Oct 2017'
#Start and #Enddates are just generic date formats.
I basically want my report to load and have the option to select whether the user wants to filter the report using the #Accountingperiod parameter or the date parameter (as both bring back entirely different data). Once the user has selected which one is to be used, the parameters (for example, #accountingperiod) will be available for selection.
How can this be achieved? Apologies if this is a silly question, I am quite new to SSRS!
Thank you

There are several ways to do this. The simplest would be to do it in the Stored Procedure and just test if Accounting Period has been set, if so use that, if not use the date range but that might not be intuitive for the user as they could select an accounting period AND a date range without realising that the date range would be ignored.
The most elegant solution would be to set the start and end dates based on the accounting period and have another entry in your accounting period list called 'Date Range' or 'Any' etc.
This assumes you can lookup or calculate the start and end dates for each accounting period. For the sake of simpicity in this example we'll assume that you have a table (say myAccPeriodTable) with the periods and date ranges in, something like this..
AccPeriodID AccPeriod StartDate EndDate
1 2017-Q1 2017-01-01 2017-03-31
2 2017=Q2 2017-04-01 2017-06-30
3 2017=Q3 2017-07-01 2017-09-30
etc...
Create a dataset (lets call it dsAccPeriods) with the following query
SELECT AccPeriodID, AccPeriod FROM myAccPeriodTable
UNION SELECT -1, 'Use Date range'
ORDER BY StartDate
You Accounting period Parameter (assume this is called #AccPeriodID) Available Values would be from a query and point to dsAccPeriods
The Value would be the AccPeriodID field and the Label field would be AccPeriod
Now create two more datasets say, dsDefaultStartDate and dsDefaultEndDate with the following queries respectively.
SELECT ISNULL(StartDate, 1999-01-01) AS StartDate From myAccPeriodTable WHERE AccPeriodID = #AccPeriodID
and
SELECT ISNULL(EndDate, 2020-12-31) AS EndDate From myAccPeriodTable WHERE AccPeriodID = #AccPeriodID
For your 'StartDate' parameter (assume its called #StartDate) the Default Value would simply point to the dsDefaultStartDate dataset and do the same with #EndDate pointing that to dsDefaultEndDate.
Now when the user selects a start date, the two data parameters should be populated with hte correct dates so your Stored Proc can simply filter based on the #StartDate and #EndDate parameters. If the user chooses the use Date Range option then the default dates will be set to whatever dates you specified in the dsDefaultStartDate and dsDefaultEndDate datasets.
Of course there is a chance that the user selects a real accounting period and then changes the date ranges. There are things you can do to avoid this but one step at a time !
I've written this answer without testing anything so there could be some mistakes but hopefully it will point you in hte right direction. If you have any issues leave a comment.

Related

Displays dates less than the current day's date

In my program, I have a data grid view. I make some amounts due for payment today. I made a display of the amounts that are due and have not been paid (late) I want a code that displays the dates less than the current date of the day I tried that following code but it only fetches the lower days and does not look For the month or year if it is greater or not than the current day's date
tbl = db.readData("SELECT * from Payments where date_batch < CONVERT(varchar(50),GetDate(), 103)", "");
DgvSearch.DataSource = tbl;
The problem with the previous code is that it doesn't fetch the date lower by day, month and year.
Fetches the date less than the current date in terms of day only I want in terms of day, month and year
Ok, so I'm going to assume date_batch is a VARCHAR(10) or similar and contains data like:
28/12/2021
29/11/2021
30/08/2021
31/12/2021
As you can see these "strings that look like dates to a human" are in order. They are not in date order, they are in alphabetical order. Big difference - SQLServer sorts strings alphabetically. When you ask for strings "less than x" it uses alphabetical sorting rules to determine "less than"-ness
Don't stores dates in a string. SQLServer has several date specific datatypes. Use them.
The following process will dig you out of the hole you've dug yourself into:
ALTER TABLE Payments ADD COLUMN BatchDate DATE;
UPDATE Payments SET BatchDate = TRY_CONVERT(Date, date_batch, 103);
Now go look at your table and sanity check it:
SELECT * FROM payments WHERE batchdate is null and date_batch is not null
This shows any dates that didn't convert. Correct their wonky bad data and run the update again.
Do another select, of all the data, and eyeball it; does it look sensible? Do you have any dates that have been put in as 02/03/2021 when they should have been 03/02/2021 etc
Now your table is full of nice dates, get rid of the strings;
ALTER TABLE Payments DROP COLUMN date_batch;
Maybe rename the column, but in SQLServer and c# WeCallThingsNamesLikeThis, we_dont_call_them_names_like_this
sp_rename 'Payments.BatchDate', 'date-batch', 'COLUMN';
Now you can do:
SELECT * FROM payments WHERE batchDate < GetDate()
And never again store dates in a string

Include records occurring within a date period

50 records in databank. I prepared a query to select contracts with ending dates between 1/1/2019 and 12/31/2020. Some of the records have dates outside the 12/31/2020; 12/31/2021. I want those records included as they were active during the queried period.
The between query only returns records with the ending date of 12/31/2020. I changed the criteria to end period between 1/1/2019 and 12/31/2021 and not 12/31/2022. That returns records before end end date of 12/31/20 and outside the start of the end period of 1/1/2019.
I've tried about 10 other things (can't remember all of them) regardless am not getting the results I need.
I'm not VBA/SQL friendly, I'm a query kind of user. Sorry if that makes my question a little more difficult.
Thank you soooo much for any direction you can give me!!
select *, DATE_FORMAT(*datetime_column*,'%m/%d/%Y') from *table_name* where *datetime_column* between '1/1/2019' and '12/31/2020'
I think the format of date leads to 'query don't satisfy correct result' problem. You could convert the date to this format and check the result

Store date range in a single column in Oracle SQL

Here trip 1 involves 2 activity_code in a single day and also concludes in a single day and most other activities are just single day but i have one trip that span over more than one day.
What could be the best possible way to store date range for that column that span more than one days.
Splitting the column into multiple begin date and end date just doesn't make sense as there would be many blank columns?
trip_id(pk,fk) Activity_code(pk,fk) date
1 a1 1st October 2015
1 a2 1st October 2015
2 a3 2nd -5th October 2015
Keep in mind that i need to search the activity_code on basis of month. such as list all the activity code that occur in October ?
Is it possible to insert a range of date in a single column or any other design solution ?
Is there any datatype that can represent the date range in single value ?
PS: oracle 11g e
Store the date ranges as FirstDate/LastDate or FirstDate/Duration.
This allows you to store the values in the native format for dates. Storing dates as strings is a bad, bad idea, because strings don't have all the built-in functionality provided for native date types.
Don't worry about the additional storage for a second date or duration. In fact, the two columns together are probably smaller than storing the value as a string.
Splitting the date into start date and end date would be ideal. Storing dates as strings is not recommended. If you store your dates as strings then there is a possibility of malformed data being stored in the column since a VARCHAR2 column will allow any value. You will have to build strong validations in your script while inserting the data which is unnecessary.
Secondly, you will not be able to perform simple operations like calculating the duration/length of the trip easily if both the start_date and end_date are stored in the same column. If they are stored in different columns it would be as simple as
SELECT trip_id, activity_code, end_date - start_date FROM trips;

Access 2007 query date calculation

I have the following Access query (see above) to give me the date, X number of weeks out from the actual WorkDte that exists in the table. But, the date these expressions calculate out may not actually exists in the “CIB_Results” table due to a bank holiday etc. Is there a way to generate the same sort of data but say if it’s running the “Wk1” calculation and it calculates out 1/1/2016 (which does not exists due to the new year holiday), instead of doing [WorkDte]-7 it will move on to [WorkDte]-14 and so on, until it finds an actual date that exists in the “CIB_Results” table? I’m wanting to apply the same logic to all the fields in the query…that way they will all self adjust based off of the actual dates that exist in the “CIB_Results” table. Any help with this would be greatly appreciated!
Have a function IsHoliday(SomeDate) that looks up SomeDate in your holiday table and returns True if found.
Then create a loop:
Public Function PreviousWorkWeekday(ByVal SomeDate As Date) As Date
Do
SomeDate = DateAdd("ww", -1, SomeDate)
Loop Until Not IsHoliday(SomeDate)
PreviousWorkWeekday = SomeDate
End Function
Now use function PreviousWorkWeekday in your query.

How to extract dates with datatye DATETIME from colum A in table X and put them into Table Y while changing datatype into DATE

Long title, easy meaning:
How is it possible to extract from a date like "2014-04-04 10:47:30.000", which is stored in one column, it's components like year, month and day?
I'm not interested in the time.
For example, I have a table called "Incidents". Inside the table we got a column called "IncidentID" and a column called "ReportingDate", in which dates like the above-mentionend are stored. Let's say we have about 50k Incidents, therefore we have also 50k dates.
A year has 365 days. I want to query for the count of the Incidents, which were reported on different dates - for instance on the 5th of October 2013.
So: How can I get the components of the date and put them into another table while having own columns for the components and how can I query for the Incidents as well?
I guess at first I have to change the datatype of the date from DATETIME to DATE, but I'm not quite sure how to go further. May anyone help me while giving me a code and explains me what it does for a sql-noob? :-)
To achieve this
I want to query for the count of the Incidents, which were reported on
different dates - for instance on the 5th of October 2013.
you haven't do this:
I guess at first I have to change the datatype of the date from
DATETIME to DATE, but I'm not quite sure how to go further.
Just query
SELECT
IncidentID
FROM incidents
WHERE ReportingDate >= '20131005'
AND ReportingDate < '20131006'