SSIS Case Statement not working - sql-server

This SSIS case expression looks perfectly valid to me, but it's coming up red. Can anybody explain why?
[Copy of "final_benefit_type_grouper"] == "MEDICAL" ? "2"

Probably because you don't have the " : {ELSE} " part of the expression. What do you want the value to be if the column does not equal "MEDICAL"?

Referring to the Microsoft Documentation, ? conditional syntax is like the following:
boolean_expression?expression1:expression2
"If the Boolean expression evaluates to TRUE, then the first expression is evaluated and the result is the expression result. If the Boolean expression evaluates to FALSE then the second expression is evaluated and its result is the expression result." Read more
so i think you are missing the second part of the expression, i might be something like this:
[Copy of "final_benefit_type_grouper"] == "MEDICAL" ? "2" : ""
Also it is better to add a NULL checking to your expression using ISNULL() function (to avoid a null value exception):
ISNULL([Copy of "final_benefit_type_grouper"])? "" : ( [Copy of "final_benefit_type_grouper"] == "MEDICAL" ? "2" : "")
It is also to follow this Link to Read more about write a case statment using a SSIS expression

Related

Can any one help me to decode Conditional(?) Operator in Derived Column of SSIS

I have to decode below expression in SQL query but very less familiar with SSIS ,so any body can help me to decode this expression.
D_UPDTD_LOAD_LOG_KEY = CRCTD_LOAD_LOG_KEY > 0 ? FINDSTRING(#[User::LV_LoadLogKey],LTRIM(RTRIM((DT_WSTR,35)CRCTD_LOAD_LOG_KEY)),1) > 0 ? CRCTD_LOAD_LOG_KEY : UPDTD_LOAD_LOG_KEY : UPDTD_LOAD_LOG_KEY
Without knowing what parts of the expression you don't understand, this answer is a little broad, but tells you what each part does. It'll then be up to you to put the information together.
Firstly let's look at this syntax:
{Boolean Expression} ? {Scalar Value} : {Scalar Value}
This, in SSIS, is an inline If. It's the equivilent of IIF in T-SQL (which is a short CASE expression). In simple terms the following expressions would be comparable:
SomeValue > Another Value ? 1 : 0
IIF(SomeValue > AnotherValue, 1, 0)
CASE WHEN SomeValue > AnotherValue THEN 1 ELSE 0 END
In your expression you have nested inline ifs, so in short hand looks like this:
{Boolean Expression 1} ? {Boolean Expression 2} ? {Scalar Value When 1 and 2 are true} : {Scalar Value when 1 is true, 2 is false} : {Scalar Value when 1 is false}
If it helps, here is a version with added parenthesis to help see where the inline ifs start and end:
({Boolean Expression 1} ? ({Boolean Expression 2} ? {Scalar Value When 1 and 2 are true} : {Scalar Value when 1 is true, 2 is false}) : {Scalar Value when 1 is false})
Now let's look at the second boolean expression:
FINDSTRING(#[User::LV_LoadLogKey],LTRIM(RTRIM((DT_WSTR,35)CRCTD_LOAD_LOG_KEY)),1) > 0
Here you have several operators. Firstly (DT_WSTR,35) converts the following expression (in this case CRCTD_LOAD_LOG_KEY) to a DT_WSTR of length 35. DT_WSTR is the equivilent to nvarchar in T-SQL. So this would be equivilent to CONVERT(nvarchar(35),CRCTD_LOAD_LOG_KEY) or CAST(CRCTD_LOAD_LOG_KEY AS nvarchar(35)) in T-SQL.
LTRIM and RTRIM do the same as their T-SQL counterparts; they remove the leading and trailing whitespace characters from a string.
Finally you have FINDSTRING, which is equivilent to CHARINDEX in T-SQL. The only difference is that the first 2 parameters are reversed; the first parameter is the string to search for the character(s) in, and the second parameter the character(s) to search for. The third parameter is the same, the occurrence number. So for a simplified version of your FINDSTRING the following would be equivalent:
FINSTRING(#[User::LV_LoadLogKey],CRCTD_LOAD_LOG_KEY,1)
CHARINDEX(CRCTD_LOAD_LOG_KEY, #LV_LoadLogKey,1)

Making the conditional operators behave like if/else statements in C

Is it possible to make the ?: operator process several statements like in the example below?
condition ? FirstTrueExpression SecondTrueExpression : FirstFalseExpression SecondFalseExpression
And is there a way to avoid specifying the 'else' statement?
condition ? TrueExpression
You can use comma operator , to concatenate multiple expressions.
The expressions are evaluated from left to right and its resulting type and value will be ones of the righthand expression.
condition ? FirstTrueExpression, SecondTrueExpression : (FirstFalseExpression, SecondFalseExpression)
Note that you need () around (FirstFalseExpression, SecondFalseExpression) due to the operator precedence while you don't need () around FirstTrueExpression, SecondTrueExpression.
You can use
condition && TrueExpression
instead of
condition ? TrueExpression
This is thanks to short-circuit evaluation of logical and && operator:
When condition is false, condition && TrueExpression will be false (0) regardless of the value of TrueExpression, and therefore TrueExpression isn't evaluated.
When condition is true, TrueExpression is evaluated because it is needed to determine the value of condition && TrueExpression.

IF statement for last value in LookupSet expression

I have an expression to get the last date value but I get '#Error' if there is no date entered. (IF part returns #Error because no date has been entered - the Else part returns a date). Here is the expression:
IIF(LookupSet(Fields!Denial_ID.Value,Fields!Denial_ID.Value,Fields!Appeal_Date_Entered.Value,"DataSet2").Length() =0, "", LookupSet(Fields!Denial_ID.Value, Fields!Denial_ID.Value, Fields!Appeal_Date_Entered.Value, "DataSet2")(LookupSet(Fields!Denial_ID.Value, Fields!Denial_ID.Value, Fields!Appeal_Date_Entered.Value, "DataSet2").Length() -1))
I'm definitely not a report writer, but there's some strange stuff in your expression which make me expect it not to work. Below is a formatted version of it (it's easier to analyse it, although I have no idea of whitespace syntax in MRB):
IIF(
LookupSet(Fields!Denial_ID.Value,
Fields!Denial_ID.Value,
Fields!Appeal_Date_Entered.Value,"DataSet2").Length() =0,
"",
LookupSet(Fields!Denial_ID.Value,
Fields!Denial_ID.Value,
Fields!Appeal_Date_Entered.Value, "DataSet2")
(LookupSet(Fields!Denial_ID.Value,
Fields!Denial_ID.Value,
Fields!Appeal_Date_Entered.Value,
"DataSet2").Length() -1)
)
According to docs, the IIF syntax is the following:
=IIF(condition, valueIfTrue, valueIfFalse)
but your expression looks like this:
IIF(condition, valueIfTrue, valueIfFalse (someOtherStuff))
so you should describe what you expect to get from that expression and change it to accord the standart syntax (see also examples on the referenced page).

Postgres NOT in array

I'm using Postgres' native array type, and trying to find the records where the ID is not in the array recipient IDs.
I can find where they are IN:
SELECT COUNT(*) FROM messages WHERE (3 = ANY (recipient_ids))
But this doesn't work:
SELECT COUNT(*) FROM messages WHERE (3 != ANY (recipient_ids))
SELECT COUNT(*) FROM messages WHERE (3 = NOT ANY (recipient_ids))
What's the right way to test for this condition?
SELECT COUNT(*) FROM "messages" WHERE NOT (3 = ANY (recipient_ids))
You can always negate WHERE (condition) with WHERE NOT (condition)
You could turn it around a bit and say "3 is not equal to all the IDs":
where 3 != all (recipient_ids)
From the fine manual:
9.21.4. ALL (array)
expression operator ALL (array expression)
The right-hand side is a parenthesized expression, which must yield an array value. The left-hand expression is evaluated and compared to each element of the array using the given operator, which must yield a Boolean result. The result of ALL is "true" if all comparisons yield true (including the case where the array has zero elements). The result is "false" if any false result is found.
Beware of NULLs
Both ALL:
(some_value != ALL(some_array))
And ANY:
NOT (some_value = ANY(some_array))
Would work as long as some_array is not null. If the array might be null, then you must account for it with coalesce(), e.g.
(some_value != ALL(coalesce(some_array, array[]::int[])))
Or
NOT (some_value = ANY(coalesce(some_array, array[]::int[])))
From the docs:
If the array expression yields a null array, the result of ANY will be null
If the array expression yields a null array, the result of ALL will be null
Augmenting the ALL/ANY Answers
I prefer all solutions that use all or any to achieve the result, appreciating the additional notes (e.g. about NULLs). As another augementation, here is a way to think about those operators.
You can think about them as short-circuit operators:
all(array) goes through all the values in the array, comparing each to the reference value using the provided operator. As soon as a comparison yields false, the process ends with false, otherwise true. (Comparable to short-circuit logical and.)
any(array) goes through all the values in the array, comparing each to the reference value using the provided operator. As soon as a comparison yields true, the process ends with true, otherwise false. (Comparable to short-circuit logical or.)
This is why 3 <> any('{1,2,3}') does not yield the desired result: The process compares 3 with 1 for inequality, which is true, and immediately returns true. A single value in the array different from 3 is enough to make the entire condition true. The 3 in the last array position is prob. never used.
3 <> all('{1,2,3}') on the other hand makes sure all values are not equal 3. It will run through all comparisons that yield true up to an element that yields false (the last in this case), to return false as the overall result. This is what the OP wants.
an update:
as of postgres 9.3,
you can use NOT in tandem with the #> (contains operator) to achieve this as well.
IE.
SELECT COUNT(*) FROM "messages" WHERE NOT recipient_ids #> ARRAY[3];
not (3 = any(recipient_ids))?
Note that the ANY/ALL operators will not work with array indexes. If indexes are in mind:
SELECT COUNT(*) FROM "messages" WHERE 3 && recipient_ids
and the negative:
SELECT COUNT(*) FROM "messages" WHERE NOT (3 && recipient_ids)
An index can then be created like:
CREATE INDEX recipient_ids_idx on tableName USING GIN(recipient_ids)
Use the following query
select id from Example where NOT (id = ANY ('{1, 2}'))

gnu C condition of "if"

we got if(expression) {...} for example. We all know if expression is true, it will execute lines in braces. But what is "True" in C?
Is that != 0 means true as I think?
Thank you
Here is what the standard has to say.
§6.8.4 Selection statements
Syntax
selection-statement:
if ( expression ) statement
if ( expression ) statement else statement
switch ( expression ) statement
§6.8.4.1 The if statement
Constraints
The controlling expression of an if statement shall have scalar type.
Semantics
In both forms, the first substatement is executed if the expression compares unequal to 0. In the else form, the second substatement is executed if the expression compares equal to 0. If the first substatement is reached via a label, the second substatement is not executed.
An else is associated with the lexically nearest preceding if that is allowed by the
syntax.
Any none-zero result tests as true.
Yes, true is not-null in C and C++.

Resources