I am working on a Attendance table in Access, where I have InTime and OutTime. These fields are of Date/Time Field.
Some records contains only Time like 11:40:00, some contain Date as well as time like 21-07-2015 11:45:00. Hence have used the below code for getting hours worked.
HrsPresent: Round(DateDiff("n",TimeValue(TimeSerial(Hour([TimeIn]),Minute([TimeIn]),Second([TimeIn]))),TimeValue(TimeSerial(Hour([TimeOut]),Minute([TimeOut]),Second([TimeOut]))))/60,2)
Using this above code, in a Column in making query gives correct Number of hours worked, but if any of the field is blank, i get #error in result.
I have tried using Nz , IsError, IsNumeric but all in Vain.
What is it that, I am doing wrong?
Is other way of getting hours worked?
You basically just need to make sure that both of the needed fields aren't blank before performing your calculation. You could do this using two IIF statements. If one field is blank then you simply assign a default value or handle it how you want.
In my example the default is zero, I have to warn you though this was done free hand and I am not super confident that my brackets line up properly. Also I am sorry that it is all on one line, I couldn't think of a logical way to break it down.
HrsPresent: IIF(Nz([TimeIn],"") = "", 0, IIF(Nz([TimeOut],"") = "", 0, Round(DateDiff("n",TimeValue(TimeSerial(Hour([TimeIn]),Minute([TimeIn]),Second([TimeIn]))),TimeValue(TimeSerial(Hour([TimeOut]),Minute([TimeOut]),Second([TimeOut]))))/60,2)))
For more information on IIF statements you can use visit here: http://www.techonthenet.com/access/functions/advanced/iif.php
Related
I am convinced that this question is NOT a duplicate of:
Cannot construct data type datetime, some of the arguments have values which are not valid
In that case the values passed in are explicitly not valid. Whereas in this case the values that the function could be expected to be called upon are all valid.
I know what the actual problem is, and it's not something that would help most people that find the other question. But it IS something that would be good to be findable on SO.
Please read the answer, and understand why it's different from the linked question before voting to close as dupe of that question.
I've run some SQL that's errored with the error message: Cannot construct data type datetime, some of the arguments have values which are not valid.
My SQL uses DATETIMEFROMPARTS, but it's fine evaluating that function in the select - it's only a problem when I filter on the selected value.
It's also demonstrating weird, can't-possibly-be-happening behaviour w.r.t. other changes to the query.
My query looks roughly like this:
WITH FilteredDataWithDate (
SELECT *, DATETIMEFROMPARTS(...some integer columns representing date data...) AS Date
FROM Table
WHERE <unrelated pre-condition filter>
)
SELECT * FROM FilteredDataWithDate
WHERE Date > '2020-01-01'
If I run that query, then it errors with the invalid data error.
But if I omit the final Date > filter, then it happily renders every result record, so clearly none of the values it's filtering on are invalid.
I've also manually examined the contents of Table WHERE <unrelated pre-condition filter> and verified that everything is a valid date.
It also has a wild collection of other behaviours:
If I replace all of ...some integer columns representing date data... with hard-coded numbers then it's fine.
If I replace some parts of that data with hardcoded values, that fixes it, but others don't. I don't find any particular patterns in what does or doesn't help.
If I remove most of the * columns from the Table select. Then it starts to be fine again.
Specifically, it appears to break any time I include an nvarchar(max) column in the CTE.
If I add an additional filter to the CTE that limits the results to Id values in the following ranges, then the results are:
130,000 and 140,000. Error.
130,000 and 135,000. Fine.
135,000 and 140,000. Fine.!!!!
Filtering by the Date column breaks everything ... but ORDER BY Date is fine. (and confirms that all dates lie within perfectly sensible bounds.)
Adding TOP 1000000 makes it work ... even though there are only about 1000 rows.
... WTAF?!
This took me a while to decode, but it turns out that the SS compiler doesn't necessarily restrict its execution of the function just to rows that are, or could be, relevant to the result set.
Depending on the execution plan it arrives at, the function could get called on any record in Table, even one that doesn't satisfy WHERE <unrelated pre-condition filter>.
This was found by another user, for another function, over here.
So the fact that it could return all the results without the filter wasn't actually proving that every input into the function was valid. And indeed there were some records in the table that weren't in the result set, but still had invalid data.
That actually means that even if you were to add an explicit WHERE filter to exclude rows containing invalid date-component data ... that isn't actually guaranteed to fix it, because the function may still get called against the 'excluded' rows.
Each of the random other things I did will have been influencing the query plan in one way or another that happened to fix/break things.
The solution is, naturally, to fix the underlying table data.
I have a query I am writing that examines an ID field and derives an ID number from that column based on several criteria. Now that I have its logic written, I want to run the query on each criteria to see if the logic is working. So, the last part of my query for doing so is as follows:
FROM TABLE1
WHERE SOURCE_SYSTEM_NM = 'XYZ' AND ((STRLEFT(SOURCE_ARRANGEMENT_ID,4)) NOT IN ('23CC','21CC'))
LIMIT 10000
Essentially what I am trying to do here is tell it to return to me only items with SOURCE_SYSTEM_NM equal to 'XYZ', while eliminating any with a SOURCE_ARRANGEMENT_ID not having the first 4 characters equal to '21CC' or '23CC'. I have a third criteria I want to filter on as well, which is that the first three characters must be '0CC'.
My problem when I run this is I get back an "Invalid Position" error. I removed one of the strings from the criteria, and it works. So, I decided to add the second in its own 'NOT IN...' clause with an AND between them, but that resulted in the same error.
If I had to guess, the NOT IN ('21CC','23CC') puts an AND between them and I think that must be the root of my issue. The criteria in my CASE statement derives the ID number with the following:
WHEN (M_CRF_CU_PRODUCT_ARRANGEMENT.SOURCE_SYSTEM_NM) IN ('XYZ') AND STRLEFT(SOURCE_ARRANGEMENT_ID, 4) IN ('23CC','21CC') THEN STRRIGHT(SOURCE_ARRANGEMENT_ID, LENGTH(SOURCE_ARRANGEMENT_ID)-4)
WHEN (M_CRF_CU_PRODUCT_ARRANGEMENT.SOURCE_SYSTEM_NM) IN ('XYZ') AND STRLEFT(SOURCE_ARRANGEMENT_ID, 3) IN ('0CC') THEN STRRIGHT(SOURCE_ARRANGEMENT_ID, LENGTH(SOURCE_ARRANGEMENT_ID)-3)
WHEN (M_CRF_CU_PRODUCT_ARRANGEMENT.SOURCE_SYSTEM_NM) IN ('XYZ') AND (STRLEFT(SOURCE_ARRANGEMENT_ID, 4) NOT IN ('23CC','21CC') OR STRLEFT(SOURCE_ARRANGEMENT_ID, 3) NOT IN ('0CC')) THEN (SOURCE_ARRANGEMENT_ID)
So with that, I am just trying to check each criteria to make sure the ID derived/created is correct. I need to filter down to get results for that last WHEN statement above, but I keep getting that "Invalid Position" in my WHERE statement at the end. I am using Aginity to run this query and it's running against an IBM Netezza database. Thanks in advance!
I figured out what the issue was on this - when performing
STRRIGHT(SOURCE_ARRANGEMENT_ID, LENGTH(SOURCE_ARRANGEMENT_ID)-4)
There are some of those Arrangement IDs that do not have 4 characters, thus I was getting an "Invalid Position". I fixed this by updating this query to use substring() instead:
SUBSTRING(SOURCE_ARRANGEMENT_ID,5,LENGTH(SOURCE_ARRANGEMENT_ID))
This fixed my issue. Just wanted to post an answer in case others have this issue. It s not Netezza specific, this will react this way with any SQL variant.
I am pulling information via a query in access. Below is my code. The results populate when the "Pinacle Type" is "DOM" or "BOOK", but if the type is anything else, as stated by the IIF statement, I get #Error.
5_BeneBankID: IIf([Pinacle_Type]="DOM" Or "BOOK",Mid(Replace(Replace([BeneABA]," ",""),"-",""),1,11),Mid(Replace(Replace([Intl_BeneBankID]," ",""),"-",""),1,11))
Embedded in this statement is are also formatting parts but those work for the first instance. The BeneABA field is a bank ABA number so this is always numeric. The Intl_BeneBankID is what is known as a SWIFT code which is either all alpha or alphanumeric. Both have a maximum length of 11 characters.
Also, If I type the following, the Intl_BeneBankID POPULATES! which is why I am stumped:
5_BeneBankID: Intl_BeneBankID
relevant table
I'm not sure, as your description of the problem seems vague to me, but if you are getting #error on some rows, but not all, you probably have null fields in your data. This is a common problem in test data, not so often in production code, but needs to be handled. Try wrapping the fields with Nz() i.e. ((Replace(Nz([Intl_BeneBankID],"")... and see what you get.
I created an IIf statement in a MS Access query, to fill null spaces in a particular field. The statement in only affecting some of the data, but not all. Why would it only work on a certain percentage of the records?
Field:Line_off_Date
Table:Claims
Criteria:IIf(IsNull("Line_off_Date "),"1/1/1900"
Here is the official Sql after delimiting:
SELECT Claims.Line_off_Date
FROM Claims
WHERE (((Claims.Line_off_Date)=IIf("Line_off_Date IsNull",#1/1/1900#,"Line_off_Date ")));
I can't screen shot onto this site, but I could give a mock up representation:
Line_off_Date
12/23/2013
12/23/2013
5/16/2010
1/1/1900
1/1/1900
12/10/2000
11/4/2008
This is listed as a column, with a space between 1/1/1900 and 12/10/2000. When I post it here, it turns into a paragraph. I hope this helps in some way...
I'm unsure about your goal, but this looks like something where you could use the Nz Function.
Criteria: Nz(Line_off_Date, "1/1/1900")
If you want IIf instead, try it this way ...
Criteria: IIf(Line_off_Date Is Null, "1/1/1900", Line_off_Date)
Note those suggestions assume Line_off_Date is text datatype. If it is actually Date/Time, delimit the value with # instead of quotes.
Criteria: Nz(Line_off_Date, #1/1/1900#)
Criteria: IIf(Line_off_Date Is Null, #1/1/1900#, Line_off_Date)
I'm more confused after seeing your WHERE clause ...
WHERE (((Claims.Line_off_Date)=IIf("Line_off_Date IsNull",#1/1/1900#,"Line_off_Date ")));
Aside from the issues of quoting and so forth, I think the logic is faulty. If you say "show me rows where Line_off_Date = something other than what is stored in Line_off_Date", you may not get any rows back.
I think you need a different approach. If you need more help, show us a brief set of sample data and your desired output based on those data.
Based on the comment discussion, my understanding is you don't really want to filter the result set based on Line_off_Date --- which means this is not a WHERE clause issue. Instead, you want to retrieve Line_off_Date values, but substitute #1900-1-1# for any which are Null. If that is correct, do the transformation with a field expression in the SELECT clause.
SELECT Nz(Claims.Line_off_Date, #1900-1-1-#) AS Line_off_Date_adjusted
FROM Claims;
I have a calculated field in one of my reports:
=IIf(Fields!Log_Total.Value > 0, (Fields!Log_Cost.Value + Fields!Labor_Cost.Value + Fields!Part_Cost.Value + Fields!Other_Costs.Value) / Fields!Log_Total.Value, 0)
As far as everything I have seen this is the correct syntax for what I am trying to accomplish, which is basically, check for zero, if not zero, add these and divide by this. When I run the report the field just comes out as #ERR. All the fields above are a part of the dataset, which I know is working fine. Please help
The expression itself seems to be fine. Otherwise you'd be getting an error right away when launchung the report.
Do one or more fields in the sum have the value null in the data source? In that case, the sum might not work.
Are you sure all the values actually have numeric types? Make sure that none of them is returned as a string or something from the database.