Enter another value when it says error in Accees - ms-access

I have IIf expression given below. Normally it works the way I want, but It gives an error when I do not enter appropriate numeric data. So, I want it to be blank or symbol, etc. when I do not enter a suitable value for the calculation. Many many thanks for your answers.
=IIf(Mid([EBGA];4;1)="/";IIf(Abs(Mid([EBGA];1;3)-400)>Abs(Mid([EBGA];5;3)-400);Format([KA1]/Mid([EBA1];InStr([EBA1];"/")+1;10);"Fixed");Format([KA1]/Mid([EBA1];1;InStr([EBA1];"/")-1);"Fixed"));Format([KA1]/[EBA1];"Fixed"))

If InStr() returns Null, Mid() will error because its position arguments must be numeric. If field used by InStr() is Null, InStr() returns Null. Handle possibility of Null.
InStr(Nz([EBA1], ""), "/")
Arithmetic with Null will return Null.

Related

How to convert YYYY-MM-DD HH:MM:SS:MS to MM/DD/YYYY in snowflake

I'm trying to convert the date field from YYYY-MM-DD HH:MM:SS:MS to MM/DD/YYYY format in a view using Snowflake database with below condition:
TO_VARCHAR(DATE(SRC_DATE),'MM/DD/YYYY')
I'm able to convert the date to expected format with above condition, but when I try to load data from this view to different table using a sp its failing with below error:
Failed: Code: 939 - State: 22023 - Message: SQL compilation error: error line 1 at position 1,058
too many arguments for function [TO_VARCHAR(VALID_FROM_DATE, 'YYYYMMDDHH24MISS.FF9')] expected 1, got 2 - Stack Trace: Statement.execute, line 9 position 58
Need help in getting the right logic to fix this error. How can I do this?
You just need to do:
TO_VARCHAR(SRC_DATE,'MM/DD/YYYY')
I’m wondering why you are trying to change the format of a date in Informatica? A date is held as a number, the format is just how it is displayed - the underlying number doesn’t change
I'm not sure exactly what's going on yet, but I can walk through some things we do know.
First, the error message says the TO_VARCHAR() function only expected one argument. Knowing this we can look at the documentation for the method. Here we see there are several overloads:
TO_VARCHAR( <expr> )
TO_VARCHAR( <numeric_expr> [, '<format>' ] )
TO_VARCHAR( <date_or_time_expr> [, '<format>' ] )
TO_VARCHAR( <binary_expr> [, '<format>' ] )
Only one of these overloads (the first) fits the error message. Most of the overloads allow multiple arguments, but only if the first argument matches certain types: numeric, date/time, or binary. This does include the expected date result of the DATE() function
Therefore we can conclude somehow the result of the DATE(SRC_DATE) call is NOT a valid <date_or_time_expr> in every case, such that we at least sometimes end up with the first overload.
While the documentation for Date() does allow several ways for the function to return NULL, it also explicitly returns a Date type:
The data type of the returned value is DATE.
Thus I'd still expect it to always match the third overload above. The only other possible result from Date() is the conversion fails, in which case we'd see a different error message entirely.
The best explanation I could guess at is the return type for Date() doesn't matter if the result is NULL (that is: NULL is inherently untyped for this purpose), such that you're still ending up with the first overload, which does not allow the 2nd argument.
You could possibly fix this by adding a COALESCE() so NULL is converted to a valid consistent throw-away date expression; something like, say, 1900-01-01. If it's important to preserve NULL values you can then in turn also wrap the whole thing in a NULLIF() call.
Finally, all of this only make sense is SRC_DATE is not already a valid <date_or_time_expr>. That is, if it's something like a varchar column. This in itself would already be a mistake in schema design. On the other hand, if it is already a datetime column, there is no need at all to call Date(), and it can be used directly with TO_VARCHAR()... but with likely the same caveat about NULL values you're already seeing.
We can test this theory by trying the following:
TO_VARCHAR(DATE(IFNULL(SRC_DATE,'1900-01-01')),'MM/DD/YYYY')

Handle both Type cast and Condition for Default in Derived Column

I have created an SSIS package where two columns of type varchar(1) have to be mapped to columns of Integer. I have this working using a Derived Column and giving both fields a type cast of (DT_I4). However, I discovered in the complete data set there are records with no value in these two fields and so I have to Type Cast AND add a condition in expression to default to "0" if null.
So far I have tried the following but are not valid
(IsNull[Notes Taken])?(DT_I4)"0":[Notes Taken]
(DT_I4)(IsNull[Notes Taken])?"0":[Notes Taken]
How do I create this expression properly
The most simple solution is to use REPLACENULL function like:
REPLACENULL([Notes Taken], "0")
And then - cast it to DT_I4. This function replaces the logic you are devising with conditional operator.
Your both formulas have errors. The most prominent - ISNULL is a function and needs parenthesis around its arguments, ISNULL([Notes Taken]), brackets only define a dataflow column. See MS Docs.
Then, your first expression
(IsNull[Notes Taken])?(DT_I4)"0":[Notes Taken]
Possibly the field [Notes Taken] is not matching data type of the DT_I4 which is the datatype of the first argument of ? : operator.
Your second expression
(DT_I4)(IsNull[Notes Taken])?"0":[Notes Taken]
Applies the data cast to the logical function ISNULL, not to the complete expression. You should put the parenthesis around the complete ? : operator like:
(DT_I4)(IsNull([Notes Taken])?"0":[Notes Taken])

Expression in query treated as string

I am having a hell of a time trying to add two calculated fields in a query together. My first record has field1= 1, and field2= 5, and the field that is trying to add them as 15!
So it’s treating them as a string.
When I try to use the function of SUM() I get an error of some of the other fields not being used in expressions, which I don’t understand.
Subtracting the two fields works as does multiplication.
I am unable to change the format of either fields in the properties as the drop down menu is blank.
Please help!
Aggregate functions act on rows not fields. Sum(field1) adds the values of field1 for group of records. Use aggregate functions in an aggregate (GROUP BY) query.
Plus (+) character will concatenate text values but add numeric. Apparently, your two fields are providing text values. Either correct the field data type or use function to convert values to number. Convert at least one field and Access will perform arithmetic instead of concatenation on all terms of expression.
Val(field1) + field2
This assumes no fields are Null. Number conversion functions will error with Null. Also, arithmetic with Null returns Null. If Null is possibility, handle with Nz().
Val(Nz(field1,0)) + Nz(field2,0)

PGSQL - No function matches the given name and argument types. You might need to add explicit type casts

This code gives an error. I have looked similar type questions and couldn't find the answer.
sum(COALESCE(((rpt.report_target_data::json->>'itemQuantity')::int)::int),0) as itemQuantity,
report_target_data is a json object and 'itemQuantity' is an element of that json. Sometimes that field contains an empty value. So when I try to get the sum it gives an error because postgres cannot get the sum if a column had a empty value. What is the wrong with the above code. It there a way to walk around that matter? Is there a way to calculate sum even if some rows contain empty values?
Here is the error of the above code ->
No function matches the given name and argument types. You might need to add explicit type casts.
In my case, it was not a COALESCE problem but I ended up in this question.
I noticed that my column values were characters (the varchar type) so what I did is:
select sum(cast(num_suf as int)) as total from results;
Just in case someone lands in this question again :)

SSIS Basic data conversion function

Consider the following statement in a Derived Column Transformation:
Derived Column Name:
EFFECTIVE_DATE
Expression:
TRIM([EFFECTIVE DATE]) == "" ? (DT_WSTR,255)NULL(DT_WSTR,255) : [EFFECTIVE DATE]
I read this as:
"If Trim of Effective Date is an empty string then assign EFFECTIVE_DATE NULL converted to a Unicode string of length 255. Otherise, assign EFFECTIVE DATE its current value."
I assume that this is what the code is doing, but I am confused about the syntax of the following:
(DT_WSTR,255)NULL(DT_WSTR,255)
I only expected:
(DT_WSTR,255)NULL
I thought that the leading (DT_WSTR,255) was a cast of the value that immediately follows, a NULL value. Why is there another (DT_WSTR,255) immeditely after. What I am missing?
That expression, per se, looks a bit redundant, as:
NULL(DT_WSTR,255)
...means "Generate a NULL value of type DT_WSTR with length 255".
So it looks like:
(DT_WSTR,255)NULL(DT_WSTR,255)
...means "Cast a NULL value of type DT_WSTR with length 255 to type DT_WSTR with length 255".
So it therefores seems at face value as if the type cast actually does nothing. However, if you leave it off, you may run into a problem as described here, which is that you'll experience this error at runtime:
For operands of the conditional operator, the data type DT_STR is supported only for input columns and cast operations. The expression "FINDSTRING([string-col],"",1) == 0 ? [string-col] : NULL(DT_STR,255,1252)" has a DT_STR operand that is not an input column or the result of a cast, and cannot be used with the conditional operation. To perform this operation, the operand needs to be explicitly cast with a cast operator.
This seems like an oddly arbitrary limitation, but it exists, so you need the cast.