Using multiple date parameters in SSRS - sql

I'm trying to build a SSRS report with multiple parameters, and when I add a standard START_DATE and END_DATE parameter I get the expected results from my SQL query line:
WHERE SOME_FIELD BETWEEN (:P_START_DATE) and (:P_END_DATE)
I would like to do something like the following scenario:
Setup:
Date Field 1 & 2: WHERE NEXT_SHIP_DUE_DATE BETWEEN (:P_NEXT_SHIP_START_DATE) AND (:P_NEXT_SHIP_END_DATE)
Date Field 3 & 4: AND PRIMARY_IMAGE_DATE BETWEEN (:P_PID_START_DATE) AND (:P_PID_END_DATE)
All date parameters in SSRS report allow NULL values and are initially set to NULL.
User should be able to:
Get all records when no date field parameters are requested
Get only records based off the date fields that user selects This may be 1 or more.
Scenarios:
Example 1: User selects no date fields - returns no data
Example 2: User selects date fields 1 & 2 only - returns no data
Example 3: User selects all date fields same date range - RETURNS CORRECT DATA
Example 4: User selects all date fields different date ranges - SSRS error: date not valid for month specified
I've tried several different pieces of SQL but no luck so far. Any help would be appreciated!

You need to add some kind of null check, be careful of brackets.
WHERE (NEXT_SHIP_DUE_DATE BETWEEN (:P_NEXT_SHIP_START_DATE) AND (:P_NEXT_SHIP_END_DATE) OR ((:P_NEXT_SHIP_END_DATE) IS NULL AND (:P_NEXT_SHIP_START_DATE) IS NULL))
AND (PRIMARY_IMAGE_DATE BETWEEN (:P_PID_START_DATE) AND (:P_PID_END_DATE) OR ((:P_PID_END_DATE) IS NULL AND (:P_PID_START_DATE) IS NULL))

Related

How to have different calculations in the same column?

I have Users on my platform and each user has a different status attached to them. What I want to achieve is that based on the Users status I want a specific calculation to be done and displayed in the same column. Based on each Users status, the calculation involves 3 dates: Last Review Date, Actual End Date and Today's Date (I will be using the GETUTCDATE() function for this but for the purpose of this post I have added a date into this column).
Below we have the 6 different Statuses and the calculations that go with each:
Draft - If the user is in this status there is no calculation and the value in the column should be NULL
Active - If the user is in this status the calculation should be: the amount of days it has been from todays date and their Last Review Date.
Completed - If the user is in this status there is no calculation and the value in the column should be NULL
Withdrawn - If the user is in this status there is no calculation and the value in the column should be NULL
Temporarily Withdrawn - If the user is in this status the calculation should be: the amount of days it has been from todays date and their Actual End Date.
Archived - If the user is in this status there is no calculation and the value in the column should be NULL
See link to image for what the table looks like Table.
I am using Microsoft SQL Server Management Studio 18.
Consider using CASE on the display part. Something like:
SELECT
(...)
,CASE
WHEN status = 'Active' THEN <calculation for status Active>
WHEN status = 'Temporarily Withdrawn' THEN <calculation for status Temporarily Withdrawn>
ELSE NULL
END AS calculation
,(...)
FROM (...)

select data from range of two dates

I want a query for selecting data between two dates (p_startdt,p_enddt) which is given by the user, if there is no input then by default data of last one year will be given as output. I am not able to put case for null or no input
where invc_dt between p_startdt and p_enddt
Use NVL to handle the case of a NULL value. The following example will take start date as a year ago if p_startdt is null and p_enddt as the current date if p_enddt is null:
WHERE invc_dt
BETWEEN NVL(p_startdt,ADD_MONTHS(SYSDATE,-12)) AND
NVL(p_enddt,SYSDATE)
Note: I'm assuming the data type of the column invc_dt is DATE.

Calculation of the difference between two dates based on multiple conditions

will be very grateful for any help you can provide related to the following situation.
I have 2 tables and 3rd table which is a joined table of those 2 tables.
Each table contains information on stage changes where for my calculation important are old value of stage field, new value, and date of change.
In case there is a only date1 in table 1 I use the following SQLite code
select *, case when `Duration1` = 0 then 1 else Duration1 end as "Duration1"
from
(select * ,
coalesce(JULIANDAY(`date2`) - JULIANDAY(`date1`), `report2: Stage Duration`)
as "Duration1"
from table)`
In ideal scenario table 1 contains project and 1st date (date1), table 2 contains project and 2nd date (date2). I can join them and I can get 3rd table with 1st and 2nd dates and calculate the difference between 2 dates there.
The complication pops up in cases when I have 2 dates in table 1 and 1 date in table 2. Here I need a help. I would like to add a condition in SQL code saying
if count(dates from report 1/date1)>count(dates from report 2/date2)
the difference should be (Duration I need) calculated as
today - max(JULIANDAY(`date1`))
This is my first question here. Thank you for you help and understanding in advance!

Ms Access Query to allow for null rows

In MS Access SQL, I have a query that uses two excel tables, one with all the data and a second as an input, the input fields are names, start date and end date. The query i wrote will return data from the first tale where the name is matched and the date falls between the range of start to end.
The problem is when the start and end date fields are left empty the query returns nothing, i need a query that when the date range is empty it will just match the name and return all dates with the matching name. I attached my query that I have so far
You want logic like this:
where (sheet1.b1_file_dd > [inputs].start_date or [inputs].start_date is null) and
(sheet1.b1_file_dd < [inputs].end_date or [inputs].end_date is null)

SSRS Null Date Multi-Filtering

I have a table with three columns: Task, Assigned To and Due Date. I want to set a filter to Due Date where every date is greater than or equal to today's date. I can't post any photos, but the filter is easy enough to understand. The filter looks like this:
Due_Date >=Today()
My problem is I want to include null or blank dates, and I'm finding this to be tricky by using SSRS filters.
How can I include all dates greater than or equal to today's date or null values?
Just add an OR with your other conditions (NULL or Blank):
=IIF(FIELDS!Due_Date.VALUE >=Today OR ISNOTHING(FIELDS!Due_Date.VALUE) OR FIELDS!Due_Date.VALUE = "", True, False)