SSIS conditional expression not handling conditional cast to NULL DT_STR - ssis

I'm having a little trouble with an SSIS expression where, in a Derived Column Transformation Data Flow Task, I am attempting to grab a 6 character substring from a string input, casting the derived columns value to NULL if it doesn't exist. This is the code I am using, with line breaks and indentation added for readability:
KeyValueLength == -2 ?
NULL(DT_STR,6,65001) :
(
KeyValueLength == -1 ?
(DT_STR,6,65001)RTRIM(SUBSTRING(StringInput,KeyValueStart,999)) :
(DT_STR,6,65001)SUBSTRING(StringInput,KeyValueStart,KeyValueLength)
)
(For reference, when KeyValueLength is -2 the key value is not found, when it is -1 then it is found at the end of StringInput, any other number and it is found in the middle of StringInput. This code works for other key values I'm getting that are casting to DT_I4 and DT_DECIMAL)
Individually, the following three expressions do not generate an error:
NULL(DT_STR,6,65001)
(DT_STR,6,65001)RTRIM(SUBSTRING(StringInput,KeyValueStart,999))
(DT_STR,6,65001)SUBSTRING(StringInput,KeyValueStart,KeyValueLength)
But when put together in that nested conditional above, I get the following error when trying to save the window:
For operands of the conditional operator, the data type DT_STR is
supported only for input columns and cast operations. The expression
"KeyValueLength == -2 ? NULL(DT_STR,6,65001) : (KeyValueLength == -1 ?
(DT_STR,6,65001)RTRIM(SUBSTRING(StringInput,KeyValueStart,999)) :
(DT_STR,6,65001)SUBSTRING(StringInput,KeyValueStart,KeyValueLength))"
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.
I'm having a little trouble figuring out exactly what the issue is here. That error message suggests it's to do with the use of conditionals, but I'm not seeing the problem.

So, in the infinite wisdom of Microsoft, this is null as a DT_STR and perfectly valid as a direct value assignment:
NULL(DT_STR,6,65001)
But if you want to assign that value in a conditional where all eventual conditions must be the same type you have to do this:
(DT_STR,6,65001)NULL(DT_STR,6,65001)
The same does not apply for other types, where something like NULL(DT_I4) is valid irrespective of whether it is directly assigned or assigned via condition. SMH

Related

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])

Reading negative numbers in a column

I'm using SSIS to separate good data from unusable date. In order to do that I used derived columns, script task and conditional split where I assigned certain conditions. One of the conditions I need to apply is that none of the numbers in one column cannot be negative. I'm guessing that the best way to solve this would be using conditional split, but I cannot get it to work. I'm new to SSIS, so any help would be appreciated.
You'd have an Expression like
[MyCaseSensitiveColumnName] < 0
and then name the output path something like BadData_NegativeValue
From the comments
that is what I did before, but I'm getting an error saying that The data types "DT_WSTR" and "DT_I4" are incompatible for binary operator ">"
That error message indicates that you are attempting to compare a unicode string (DT_WSTR) and an integer (DT_I4) and that the expression language does not allow it.
To resolve this type incompatibility, you would need to first convert the value of MyCaseSensitiveColumnName from DT_WSTR to an integer.
I'd likely add a Derived Column Component to my data flow and create a new column called MyCaseSensitiveColumnNameAsInteger with an expression like
(DT_I4) [MyCaseSensitiveColumnName]
Now, that may be perilous depending on the quality of your source data. I don't know why you are pulling numeric data in as a string. If there could be non whole numbers in the data set, then we will need to check before making the cast. If there are NULLs in that dataset, those too may cause issues.
That would result in our conditional split check becoming
[MyCaseSensitiveColumnNameAsInteger] < 0

Lookup function error in SSRS with operators

=Lookup(Fields!ID.Value+Fields!Name.Value+Fields!Age.Value,Fields!ID.Value+Fields!Name.Value+Fields!Age.Value,Fields!Amount.Value,"Table2")
=Lookup(Fields!ID.Value &Fields!Name.Value &Fields!Age.Value,Fields!ID.Value &Fields!Name.Value &Fields!Age.Value,Fields!Amount.Value,"Table2")
While using above two expressions in ssrs its displaying error like :
An Unexpected error occurred while compiling expression. Native
compiler returns value: Operator + is not defined for type 'object'
and
'Microsoft.ReportingServices.ReportProcessing.ReportObjectModel.Field'.
An Unexpected error occurred while compiling expression. Native
compiler returns value: Operator & is not defined for type 'object'
and
'Microsoft.ReportingServices.ReportProcessing.ReportObjectModel.Field'.
Please provide a solution to run lookup with multiple columns.
Thanks You.
The first step I would take would be to cast every field value as a string. To do that addCStr(<Value>) around every field value. My assumption is that SSRS doesn’t like concatenating numbers and strings.

SSRS FormatNumber Error

trying to format number (decimal), but it shows me #Error on production server:
=FormatNumber(First(Fields!SumWithMailDelivery.Value, "document"), 0)
but on developers server it works fine.
Maybe someone know, how can i resolve this?
p.s. without formatting it works on production server fine too.
As #IanPreston says, it is most likely a type conversion error. I imagine your production data has some invalid characters for that column or some Null columns that make the numeric conversion fail.
You can use the Val function to do the conversion. Val differs from other numeric conversion functions in that it won't error when the string to be converted isn't numeric - it just does the best job it can.
So try an expression like this for the Value property:
=IIF(Fields!SumWithMailDelivery.Value Is Nothing,
Nothing,
IIF(IsNumeric(Fields!SumWithMailDelivery.Value),
Val(Fields!SumWithMailDelivery.Value),
Fields!SumWithMailDelivery.Value)
)
then use N0 as the Format property to format it as numeric if possible.
This formula will:
Leave the cell as Nothing if the field is Null
Convert to numeric and use the appropriate format if possible
Otherwise just output whatever is in the field

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.