My Query is not giving the right results in access - sql

SELECT (SignIn.VisitorFirstName & " " & SignIn.VisitorLastName) AS [Visitor Name], SignIn.SignInDateTime AS [Sign In Time], ([UserList.FirstName] & " " & [UserList.LastName]) AS Reason
FROM SignIn INNER JOIN UserList ON SignIn.AssignedPO = UserList.POid
WHERE (((SignIn.Complete)=No) AND ((Format([SignInDateTime],"Short Date")) Between #8/8/2016# And #8/10/2016#))
ORDER BY SignIn.SignInDateTime;
I am getting values for dates outside the scope.
The query runs but returns the wrong values. (see image below)
**
EDIT from comments: It's returning values before August 8th
**
Properties of my DateTime field (SignInDateTime)

Not sure what this conversion/comparison is doing
((Format([SignInDateTime],"Short Date"))
You might be safe to use CDate or just the Field itself - it looks like it's a date time field anyways
If it is a Date/Time field, try changing this
((Format([SignInDateTime],"Short Date"))
To this
([SignInDateTime]

Related

SSRS lookup with datediff Incorrect result error showing in fields

I have to write again this problem. I am not an expert in SSRS.
I have been trying to resolve this issue. Even though the fields are all the same datatype am still getting error and incorrect time difference in the filed in some fields.
I do not know what am doing wrong. This is the code. Please find below screen shot also.
enter code =Datediff(DateInterval.Minute, Fields!health_start_date.Value, Lookup(Fields!flight_date.Value & Fields!register_number.Value & DATEPART(DateInterval.Hour,Fields!health_start_date.Value) , Fields!FL_DATE.Value & Fields!REG.Value & DatePart(DateInterval.Hour, Fields!ATD.Value), Fields!ATD.Value, "DataSetAIMS") ) MOD 60 & " mins "
here
The scenario is that the two fields Health start date and ATD have two different dataset which shows time of flight differently but the time difference is always in minutes. What am trying to do is to compare the two table with their hour and date using lookup to find difference in minutes
Okay, I think I see the problem: you're really close and this stuff trips us all up on occasion. In the LOOKUP function, I think you are joining the two data sets based on:
flight_date = FL_DATE, and
register_number = REG, and
HOUR(health_start_date) = HOUR(ATD)
Which is fine, but the ampersand (&) just "concatenates" strings together, and (a) your strings don't actually match, and (b) HOUR is a number, not a string.
(a) The "date" in the first data set is DDMMYYYY, and in the 2nd it is DDMMYY. I.e., "05222018" is not the same as "052818", so your lookup will not work. It's possible that they are stored correctly as date fields in the database and so the comparison will work fine, but better to force them to match. For that I'd recommend formatting each of the date strings into a standard format, like "FormatDateTime(Fields!flight_date.Value, DateFormat.VBShortDate)".
(b) Should be easy, just add ".ToString" after the parentheses, like "DATEPART(DateInterval.Hour,Fields!health_start_date.Value).ToString()".
(c) I'm assuming the register_number and REG are formatted the same.
Given all that, your new LOOKUP function might look something like this:
=Datediff(DateInterval.Minute,
Fields!health_start_date.Value,
Lookup(
FormatDateTime(Fields!flight_date.Value, DateFormat.VBShortDate) & Fields!register_number.Value & DATEPART(DateInterval.Hour,Fields!health_start_date.Value).ToString() ,
FormatDateTime(Fields!FL_DATE.Value, DateFormat.VBShortDate) & Fields!REG.Value & DatePart(DateInterval.Hour, Fields!ATD.Value).ToString()
, Fields!ATD.Value
, "DataSetAIMS")
).ToString() & " minutes"
However, I think you'd make your life easier by just doing that sort of calculation in the database layer, in the report query, something like:
SELECT
d1.FlightDate
, d1.RegisterNumber
, d1.HealthStartDate
, d2.ATD
, DiffInMinutes = DATEDIFF(MINUTE, d1.HealthStartDate, d2.ATD)
FROM DataSet1 d1
JOIN DataSetAIMS d2
ON CAST(d1.FlightDate AS DATE) = CAST(d2.FL_DATE AS DATE)
AND d1.register_number = d2.REG
AND DATEPART(HOUR, d1.health_start_date) = DATEPART(HOUR, d2.ATD)
This code certainly isn't exact, but hopefully it will get you pointed in the right direction!
Thanks Russel. I have accepted your answer. What I did was to remove the formatdate from the expression code and modify the query design code to convert to the same time date : the code below
=Datediff(DateInterval.Minute, Fields!health_start_date.Value, Lookup(Fields!register_number.Value & DATEPART(DateInterval.Hour,Fields!health_start_date.Value).ToString() , Fields!REG.Value & DatePart(DateInterval.Hour, Fields!ATD.Value).ToString(), Fields!ATD.Value, "DataSetAIMS") ).ToString() MOD 60 & " mins "
or the below as well works :
=Datediff(DateInterval.Minute,
Fields!health_start_date.Value,
Lookup(
Format(Fields!flight_date.Value, "ddMMyy") & Fields!register_number.Value & DATEPART(DateInterval.Hour,Fields!health_start_date.Value).ToString() ,
Format(Fields!FL_DATE.Value, "ddMMyy") & Fields!REG.Value & DatePart(DateInterval.Hour,Fields!ATD.Value).ToString()
, Fields!ATD.Value
, "DataSetAIMS")
).ToString() MOD 60 & " minutes

Access: Passing Parameters from Form to Query

I'm writing a query in Access 2013. The query will use the parameters from a form, pass those parameters to the query, and then display the results. I almost have it working. The problem with this form is that there are multiple date fields. There are two text fields (txtFromDate and txtToDate).
The idea is that user can enter date range one of two ways:
the user can click the date picker and select a date range to pass to the query;
the user can choose the Fiscal Year from a combobox.
I have created a separate table that is setup as follows:
Table: tbl_FiscalYear
Columns: FiscalYr_ID, FiscalYearName, FromDate, ToDate
In my query, the results work great (all parameters passed to query) if the user enters the manual dates in the txt fields. However, if they bypass the manual date entry and use the Fiscal Year combobox, the query results ignore all previous parameters and the results only reflect the appropriate date range per the Fiscal Year date setup in the fiscal year table.
I think the culprit is the OR statement at the end. Is there a way to incorporate both date search parameters into the form, and allowing the use of one or the other? Is my code not correct since it doesn't allow passing null values from the text date fields?
SELECT tbl_QUOTE.QUOTE_ID,
tbl_QUOTE.QUOTE_DATESTART,
tbl_QUOTE.QUOTE_DATEEND,
tbl_QUOTE.QUOTE_lookup_POC_ID,
tbl_QUOTE.QUOTE_lookup_LOC_ID,
tbl_QUOTE.QUOTE_lookup_TRAINER_NAME,
tbl_QUOTE.QUOTE_lookup_STATUS_ID,
tbl_QUOTE.QUOTE_lookup_TRAINER_ID,
tbl_QUOTE.QUOTE_lookup_MODS_ID,
tbl_POC.POC_AGENCY_lookup,
tbl_QUOTE.QUOTE_FINANCIAL_CD,
tbl_FISCALYEAR.FISCALYEARNAME
FROM tbl_fiscalyear, tbl_QUOTE
INNER JOIN tbl_POC ON tbl_QUOTE.QUOTE_lookup_POC_ID = tbl_POC.POC_ID
WHERE Nz([QUOTE_lookup_STATUS_ID],"") Like [Forms]![frm_QuoteReport]![cboStatusLookup] & "*"
AND Nz([tbl_POC].[POC_AGENCY_lookup],"") Like [Forms]![frm_QuoteReport]![cboAgency] & "*"
AND Nz([QUOTE_lookup_POC_ID],"") Like [Forms]![frm_QuoteReport]![cboPOC] & "*"
AND Nz([QUOTE_lookup_MODS_ID],"") Like [Forms]![frm_QuoteReport]![cboCourse] & "*"
AND Nz([QUOTE_lookup_LOC_ID],"") Like [Forms]![frm_QuoteReport]![cboLocation] & "*"
AND Nz([QUOTE_lookup_Trainer_ID],"") Like [Forms]![frm_QuoteReport]![cboTrainer] & "*"
AND Nz([Fiscalyr_ID],"") Like [Forms]![frm_QuoteReport]![cboFY] & "*"
AND Nz([Quote_Financial_Cd],"") Like [Forms]![frm_QuoteReport]![chkFundCd] & "*"
AND [QUOTE_DATESTART] Between [Forms]![frm_QuoteReport]![txtFromDate]
And [Forms]![frm_QuoteReport]![txtToDate]
OR [QUOTE_DATESTART]
BETWEEN (SELECT [tbl_fiscalyear].[fromdate]
FROM tbl_fiscalyear
WHERE fiscalyr_ID = [Forms]![frm_QuoteReport]![cboFY])
AND (SELECT [tbl_fiscalyear].[todate]
FROM tbl_fiscalyear
WHERE fiscalyr_ID = [Forms]![frm_QuoteReport]![cboFY]);
I believe I resolved it. I edited the final lines as follows:
OR [QUOTE_DATESTART] Between [Forms]![frm_QuotesReports]![txtFromDate]
And [Forms]![frm_QuotesReports]![txtToDate] <br/> AND [QUOTE_DATESTART]
BETWEEN (SELECT [tbl_fiscalyear].[fromdate] FROM tbl_fiscalyear WHERE
fiscalyr_ID = [Forms]![frm_QuotesReports]![cboFY]) AND (SELECT
[tbl_fiscalyear].[todate] FROM tbl_fiscalyear WHERE fiscalyr_ID = [Forms]!
[frm_QuotesReports]![cboFY])

How do I format the date to include the week number?

I am using Microsoft Business Intelligence Development Studio 2012 trying to create a calculated field that will give me the Month and Week number of a field with the name 'createdon'.
I am new to SQL and after some research this is the code that I have come up with.
=Format(Fields!createdon.Value, "MMMM") & " Week " & (Int(DateDiff("d",DateSerial(Year(Fields!createdon.Value),Month(Fields!createdon.Value),1), Fields!FullDateAlternateKey.Value)/7)+1).ToString
But I am getting the error:
"The Field expression for the dataset 'Dataset1' refers to the field
'FullDateAlternateKey'. Report item expressions can only refer to
fields within the current datasetscop or, if inside an aggregate, the
specified dataset scope. Letters in the names of fields my use the
correct case.
I know this means that "FullDateAlternateKey" is not a valid field name, but I do not know what field name to substitute instead to correct this expression. Does anyone with more experience have any ideas as to how I can correct this?
Ideally, I would like it formatted to have a column that shows "week ending in ".
So any dates from 9/26/2015 to 10/2/2015 would say "Week of 10/2/2015"
Try this:
="Month: " & Datepart("m", Fields!createdon.Value) & " Week: " &
Datepart(DateInterval.WeekOfYear,Fields!createdon.Value)
You will get the following: Month: 10 Week: 43
Edit your question and add a example of the desired result in order to help you with the specific format.
Update:
Try this:
="Today: " & Format(Fields!createdon.Value,"dd/M/yyyy") & "Week Of: " & Format(
DATEADD("d" ,7-DATEPART(DateInterval.WeekDay,Fields!createdon.Value,FirstDayOfWeek.Monday),Fields!createdon.Value),
"dd/M/yyyy")
It will show: Today: 19/10/2015 Week Of: 25/10/2015

Contains Syntax for SQL

I'm at my wits end here, I'm trying to get this to work
Set rex2 = db.OpenRecordset(" Select count(*) from events where event_date >= #" & Format(last_week_start, "mm/dd/yyyy") & "# and maildate <= #" & Format(last_week_end, "mm/dd/yyyy") & "# and contains(event_type, ""1st call attempted"") and work_ID contains ""UNS"";")
where event_type is the column name in the database and work_ID is also the column name in the database. I've tried it in numerous ways i.e.
WHERE event_type contains ""1st Call Attempted"
etc but I'm having no luck.
I'd change my code but in event_type there are way too many 1st call attempted categories to list.
I'm also open to using a left statement ie
Where left(event_type, 18) = " 1st Call attempted"
Anything to get this sodding thing working
Please help me.
Perhaps you should use the LIKE operator. You'd need to do something like this:
... WHERE event_type LIKE '1st Call attempted%' ...
and similarly for work_ID.
This will match any string that starts with "1st Call attempted" and ends in whatever, since it's like a * wildcard. If you executed it in Access, then you'd use a * instead of a %, but in OLEDB you need to use a %.

Docmd.openreport Where Clause syntax

I am trying to save individual records in a report to PDF files. using Access 2010. I got this to work, but I need to put a start date and end date in, and I can't seem to figure out the syntax. Here is what I have so far:
DoCmd.OpenReport "Rpt Form Responses", acViewReport, , "[Facility Number]=" & temp & _ And [Service Date] Between & begindate And enddate;
The top line works fine but when I add the second I can't get it to work. I have tried all the qualifiers', ", and # and many different variations of syntax with no luck.
begindate and enddate are strings that I capture from an input box. Should I change these to a date? I have done a lot of reading on here and based on suggestions I thought about putting a text box on the form for begin and end but I think I would rather do the inputbox.
Are you sure the syntax you posted above is accurate, because it is not correct.
I am assuming 'temp' is numeric, else you need to enclose in single-quotes;
Tthe continuation line is missing the leading " followed by a space;
Missing quotes, spaces around your BETWEEN and AND
You can get away with strings for the dates as long as you did something like:
begindate = #1/1/2005#
enddate = #1/1/2012#
Here is what I believe your syntax should look like (I tested and it works):
DoCmd.OpenReport "Rpt Form Responses", acViewReport, , "[Facility Number]=" & temp & _
" And [Service Date] Between #" & begindate & "# And #" & enddate & "#"