User is prompted for same query parameter more than once - sql

I have a parameter query selecting information from my table based on the start date entered by the user and today's date. I want to search two fields in my table with the WHERE statement without entering the prompt for the starting date twice. Right now I have:
SELECT PatientSurvey.PatientID
FROM PatientSurvey
WHERE (PatientSurvey.[6MonthSurveyReturn] OR PatientSurvey.[12MonthSurveyReturn]) Between [Enter the last date checked for:] And Date();
This doesn't seem to work. But if I do the following:
SELECT PatientSurvey.PatientID
FROM PatientSurvey
WHERE (PatientSurvey.[6MonthSurveyReturn]) Between [Enter the last date checked for:] And Date() OR (PatientSurvey.[12MonthSurveyReturn]) Between [Enter the last date checked for:] And Date();
Then it prompts the user twice for the same input. How can this be prevented?

Add a PARAMETERS declaration to your query.
PARAMETERS [Enter the last date checked for:] DateTime;
SELECT PatientSurvey.PatientID
FROM PatientSurvey
WHERE
(([PatientSurvey].[6MonthSurveyReturn]) Between [Enter the last date checked for:] And Date())
OR (([PatientSurvey].[12MonthSurveyReturn]) Between [Enter the last date checked for:] And Date());

Related

Set calendar dates

I am using DatePicker and I want to set a calendar date based on other calendar field, if the date of first calendar falls in current month then the date of second calendar should be valid from the date of first calendar (for eg: 1st calendar date is set to 20-06-2021 which is of current month then the second calendar date should allow to add date from 20-06-2021), if the date of first calendar falls in last month then the date of second calendar should be valid from the start date of this month (for eg: 1st calendar is set to 20-05-2021 which is of last month then the second calendar date should allow to add date from 01-06-2021), please give me a solution on how I can resolve this issue.
You can do it by using state suppose you have started your first date picker from 2021-06-20
this.state = {date:"2021-06-20"}
and then when the user changes the date you update your state by
onDateChange={(date) => {this.setState({date: date})}}
now by using this updated state you can start your second date picker by using the current state.
And by assigning it to a variable you can apply any conditional statements that you want.

Access Date Query Problem for Today's Date

I have a set of data that includes a field called ReleaseDate, type of Date/Time, that gets written using NOW() at the time a record is added.
I can open the table and filter for today's date and get a result of 152 records.
I can run this following query, enter today's date at the prompts, and also get a result of 152 records.
SELECT ProductData.ReleaseDate, ProductData.Shift, ProductData.ExtrusionLine, ProductData.RollDensity
FROM ProductData
WHERE (((ProductData.ReleaseDate) Between [Please Enter Start Date mm/dd/yyyy] & " " & #12/30/1899# And [Please Enter End Date mm/dd/yyyy] & " " & #12/30/1899 23:59:59#));
If I run the following query no records are retrieved. I cannot figure out why.
SELECT ProductData.ReleaseDate, ProductData.Shift, ProductData.ExtrusionLine, ProductData.RollDensity
FROM ProductData
WHERE (((ProductData.ReleaseDate)=Date()));
From what I have read Date() as a criteria should return any records with today's date. Prompting for the date is not an option.
In addition to Gutav's answer, better use:
WHERE ProductData.ReleaseDate >= Date() AND ProductData.ReleaseDate < DateAdd("d", 1, Date())
as this can use an index on ProductData.ReleaseDate (faster). See my answer on Unable to use today's date as a criteria in a query
It's because ReleaseDate contains a time part (from Now()). You can strip that:
WHERE Fix(ProductData.ReleaseDate) = Date();

Changing the date used within a query which has been provided by a parameter

I am trying to create a query which will be used within a reporting dashboard. the query needs to pull a date from a parameter which is feed from a datetime control within the dash board.
The issue i have is that the date time control will show the current date or can be changed using a drop down calender. I need the query to look at the date provided and perform a bit of an if statement.
If the date provided is the same month as the current month. I need the date used within the query to be the last day of the previous month. ELSE it should use the last day of the month selected.
I have tried several variations writing my code into the Where Clause but have so far not come to any success. Even just trying to get the query to give me the last day of the previous month (Code below) has not worked.
Any ideas would be welcome. I am really stuck on this.
SELECT A.ETLKEY, A.EFFECTIVEDATE, A.ACTUAL,
FROM DBO.View
WHERE (A.EFFECTIVEDATE = (DateAdd("d", -1, CDate(
"01/"+CStr(Format(&EndDate(),"MM/yyyy"))))))
Second attempt
DECLARE #dtDate DATE
SET #dtDate = &Enddate
--'Month/day/year'
SELECT Case
When #dtDate = '08/01/2016'
-- Last day of previous month, as this month will have no date in it
Then DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,#dtDate),0))
-- Last day of month stated.
Else DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,#dtDate)+1,0))
So final working Code for anyone who might find this useful in the futrue
DECLARE #dtDate DATETime
SET #dtDate = &Enddate
--'Month/day/year'
SELECT Case
--compares month from parameter to todays month
When DATEPART(mm,#dtDate) = DATEPART(mm,GETDATE())
-- Last day of previous month, as this month will have no date in it
Then DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,#dtDate),0))
-- Last day of month stated.
Else DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,#dtDate)+1,0))
END

SSRS optional cascading parameter for date

I have a report that I am attempting to setup a subscription for. The subscriber would like the report on the 4th every month for the previous month's end date. e.g ran today would return the data through 10/31.
I would like to create this report so that there is a cascading parameter that would be labeled Use previous month end date to set the end date to the subscription date when flagged on and allow the user to enter a date when flagged off. so that i can use this parameter to create the dynamic subscription but allow anyone else hitting it through the report server to enter their own date.
Is this possible?
You don't need the UsePriorMonth parameter - just default the date range to be the prior month and allow users to change it if they want.
So your DateFrom parameter Default Value expression is:
=DateAdd(DateInterval.Month, -1, DateAdd(DateInterval.Day, 1-Day(Today), Today))
and your DateTo parameter Default Value expression is:
=DateAdd(DateInterval.Day, -1, DateAdd(DateInterval.Day, 1-Day(Today), Today))

Check whether date falls in the date range of the current year in sql

Suppose i have an input start date as "01/01/2014" and input end date as "31/01/2014". I need to check whether the input start date and input end date falls within highlighted record.In the highlighted record i have two columns monthfrom and monthto.Which is from December to February.But in my table there is no year mentioned.I need to take for current year but if i take current year then my query fails
SELECT * FROM tablename WHERE dateformatted BETWEEN '01/12/2014' AND '28/02/2014'
If i take end date as next year also again my query fails
SELECT * FROM tablename WHERE dateformatted BETWEEN '01/12/2014' AND '28/02/2015'.
Suppose i format the highlighted details as above query then i can check the input date falls within the date range.After checking the input date falls in the highlighted record i need to get number of days using weekfrom and weekto column.If i use this query
SELECT * FROM tablename WHERE dateformatted BETWEEN '01/12/2014' AND '28/02/2015'.
then will get wrong result because weekfrom will be different in the current year and next year.
Suppose i use the query like this
SELECT * FROM tablename WHERE dateformatted BETWEEN '01/12/2014' AND '28/02/2014'.Again i will not get the expected result because am checking for December to February of the current year.Please help me i need to check two conditions
Whether input date falls in the highlighted record
Count the number of days if it falls within the highlighted record with weekfrom and weekto keeping into consideration.
Week from and weekto may be in reverse order also as saturday to sunday(7 to 1).Here also we need to count the number of working days.
Instead of using this:
SELECT * FROM tablename WHERE dateformatted BETWEEN '01/12/2014' AND '28/02/2014'
Use this:
SELECT * FROM tablename WHERE dateformatted BETWEEN #01/12/2014# AND #28/02/2014#