Case or if statement in SSIS expression - sql-server

I have a project parameter in SSIS that I want to use to be able to change our data source connections.
I'm trying to write an expression in the connection properties but I'm coming up short. I want to be able to evaluate a few different values and return different values if true
Here's what I have
#[$Project::Parameter] == "SERVER1" ? #[$Project::SERVER1_ConnectionString]
: ( #[$Project::Parameter] == "SERVER2" ? #[$Project::SERVER2_ConnectionString]
: ( #[$Project::Parameter] == "SERVER3" ? #[$Project::SERVER3_ConnectionString]
: "Unknown Server"))
I've tried adding in other servers to this code and I must not be understanding how you can evaluate additional results and set additional values
Basically, I'm looking to do this
If Parameter = SERVER1 then give me SERVER1_ConnectionString, if Parameter = SERVER2, then give me SERVER2_ConnectionString and so on...
EDIT: The syntax was correct but my issue was having a typo my project parameter

Evaluate variable as Expression
The easiest way is to Add a Variable #[User::ConnectionSting] of type String, Select to Evaluate this variable as expression, and use the following expression:
#[$Project::Parameter] == "SERVER1" ? #[$Project::SERVER1_ConnectionString]
: ( #[$Project::Parameter] == "SERVER2" ? #[$Project::SERVER2_ConnectionString]
: ( #[$Project::Parameter] == "SERVER3" ? #[$Project::SERVER3_ConnectionString]
: ""))
Then Click on the OLEDB Connection Manager, press F4 to Show the properties Tab, GoTo Expression, Select the ConnectionString property and use the following expression:
#[User::ConnectionSting]
And Click on the data flow task and the tasks that uses the connection and Set the Delay Validation property to True
Using Expression Task
You can use the same method but instead of evaluating #[User::ConnectionSting] as expression, add an Expression Task at the package beginning and use the following expression:
#[User::ConnectionSting] = (#[$Project::Parameter] == "SERVER1" ?
#[$Project::SERVER1_ConnectionString] : ( #[$Project::Parameter] == "SERVER2" ? #[$Project::SERVER2_ConnectionString] : ( #[$Project::Parameter] == "SERVER3" ? #[$Project::SERVER3_ConnectionString] : "")))

Related

ssis derived column - check if date field is null

In SSIS derived column I need to check if a Date field is empty or null, I'm using this expression but I got an error:
TRIM([date field]) == "" ? NULL(DT_DATE) : TRIM([date field])
The field is of DT_DATE data type. What's wrong in that?
I've used expressions like this successfully:
TRIM(String_field) == "" ? NULL(DT_WSTR,255) : TRIM(String_field)
You are combining data types (if true is a date and false is a string).
This is the correct formula:
TRIM([date field]) == "" ? NULL(DT_DATE) : (DT_DATE)TRIM([date field])
I am assuming [date field] is a string.
Concerning the error, it is related to the derived column result data type, since in conditional operators (?:) both true and false expression must generate the same data type.
In addition, your expression doesn't check for NULL values, you should use the following expression:
TRIM(REPLACENULL([date field],"")) == "" ? NULL(DT_DATE) : (DT_DATE)TRIM([date field])
Also, you have to make sure that [date field] can be parsed as date, so (DT_DATE)TRIM([date field]) will not throw an exception, you can use the error output to handles the value that doesn't contain a valid date.

Nested IF ELSE in a derived column

I have the following logic to store the date in BI_StartDate as below:
If UpdatedDate is not null then BI_StartDate=UpddatedDate
ELSE BI_StartDate takes EntryDate value , if the EntryDate is null
then BI_StartDate=CreatedDate
If the CreatedDate IS NULL then BI_StartDate=GetDATE()
I am using a derived column as seen below:
ISNULL(UpdatedDateODS) ? EntryDateODS : (ISNULL(EntryDateODS) ? CreatedDateODS :
(ISNULL(CreatedDateODS) ? GETDATE() ))
I am getting this error:
The expression "ISNULL(UpdatedDateODS) ? EntryDateODS :
(ISNULL(EntryDateODS) ? CreatedDateODS :(ISNULL(CreatedDateODS) ?
GETDATE() ))" on "Derived Column.Outputs[Derived Column
Output].Columns[Derived Column 1]" is not valid.
You are looking the first non-null which is a coalesce which doesn't exist in SSIS Data Flow (derived Column).
I'd suggest a very simple script component:
Row.BIStartDate = Row.UpdateDate ?? Row.EntryDate ?? Row.CreatedDate ?? DateTime.Now;
This is the Input Columns Screen:
This is the Inputs and Outputs:
And then you add the above code to Row Processing section:
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
/*
* Add your code here
*/
Row.BIStartDate = Row.UpdateDate ?? Row.EntryDate ?? Row.CreatedDate ?? DateTime.Now;
}
From syntax perspective, the nested if-else condition is not written well, since you have to make sure that all possible output should have the same data type, also you didn't mentioned the last "else" condition:
ISNULL(UpdatedDateODS) ? EntryDateODS : (ISNULL(EntryDateODS) ? CreatedDateODS :
(ISNULL(CreatedDateODS) ? GETDATE() : **<missing>** ))
From logical perspective, you the expression may throw exception since you are using EntryDateODS column if ISNULL(UpdatedDateODS) is true, while you should check if EntryDateODS is not null before using it, I suggest that the expression is as following:
ISNULL(UpdatedDateODS) ? UpdatedDateODS : (ISNULL(EntryDateODS) ? EntryDateODS :
(ISNULL(CreatedDateODS) ? CreatedDateODS : GETDATE() ))
As mentioned above, if UpdatedDateODS , EntryDateODS, CreatedDateODS and GETDATE() don't have the same data type then you should cast to a unified data type as example:
ISNULL(UpdatedDateODS) ? (DT_DATE)UpdatedDateODS : (ISNULL(EntryDateODS) ? (DT_DATE)EntryDateODS :
(ISNULL(CreatedDateODS) ? (DT_DATE)CreatedDateODS : (DT_DATE)GETDATE() ))

Case test if substring exist and replace with blank

I need to cleanup a set of companies name by replacing : INC, LTD, LTD. , INC. , others, with a empty space when they are individual words ( with one blank space before the word i.e. Incoming INC) and not letters part of company name i.e. INComing Money.
The logic I tried :
case
when FINDSTRING([Trade Name]," INC",1) > 0 then REPLACE([Trade Name]," INC","")
when FINDSTRING([Trade Name]," LTD",1) > 0 then REPLACE([Trade Name]," LTD","")
ELSE [Trade Name]
I tried SSIS expresion in a derived column :
FINDSTRING( [Trade Name] ," INC",1) ? REPLACE([Trade Name]," INC","") :
FINDSTRING([Trade Name]," LTD",1) ? REPLACE([Trade Name]," LTD",""):
The error received:
Error at Data Flow Task [Derived Column [1]]: Attempt to find the
input column named "A" failed with error code 0xC0010009. The input
column specified was not found in the input column collection.
In a similar case it is easier to use a Script Component to clean this column, you can simply split the column based on spaces then re concatenate the parts that are not equal to INC, you can use the following method to do that, or you can simple use RegEx.Replace() method to replace values based on regular expressions:
string value = "";
string[] parts = Row.TradeName.Split(' ');
foreach(string str in parts){
if(str != "INC"){
value += " " + str;
}
}
Row.outTradeName = value.TrimStart();

Defined expression in SSRS report is displaying #ERROR

I have defined an expression in one of my cells from my dataset1. My Design window and I have to repeat for each month's cells but I'm getting an #ERROR when I click on the PREVIEW tab in SSRS.
My thought is if the ActivityMonth value = 1 and the Type value = "PIF" then display the value in the Data column.
=IIF(Fields!Activity_Month.Value = 1 AND Fields!Type.Value = "PIF", Fields!Data.Value, 0)
I got this WARNING from SSRS:
[rsRuntimeErrorInExpression] The Value expression for the textrun ‘Textbox1471.Paragraphs[0].TextRuns[0]’ contains an error: Input string was not in a correct format.
But it ran successfully.
From the comments and the edit history, it looks like you have used & mark which is used to concatenate strings instead of AND keyword. After editing the expression - for me - the following expression looks great:
=IIF(Fields!Activity_Month.Value = 1 AND Fields!Type.Value = "PIF", Fields!Data.Value, 0)
But i have two remarks:
It may cause an error due to the different data types returned by the expression (0 is integer, Data.Value has another data type:
If Fields!Data.Value is of type string then use the following expression:
=IIF(Fields!Activity_Month.Value = 1 AND Fields!Type.Value = "PIF", Fields!Data.Value, "0")
Another thing to mention is that if value contains null it may throw an exception, so you have to check if field contains null:
SSRS expression replace NULL with another field value
isnull in SSRS expressions

SSIS Expression equivalent to SQL Expression

what is the SSIS equivalent expression(Derived Column component) of the below SQL expression
cast(CASE WHEN len(cast(KPI as varchar(3))) > 2 THEN
CASE substring(cast(KPI as varchar(3)),3,1)
WHEN 1 then left(cast(KPI as varchar(3)),1) + 'a'
WHEN 2 then left(cast(KPI as varchar(3)),1) + 'b'
WHEN 3 then left(cast(KPI as varchar(3)),1) + 'c'
WHEN 4 then left(cast(KPI as varchar(3)),1) + 'd'
END
ELSE cast(KPI as varchar(3))
END as VarChar(3)) as 'ColumnName'
here Kpi column is a double precision floating point data type...
one major thing i have observed here is LEFT String function is missing from SSIS Expression Builder.
SSIS experts please have a look..
I have been trying around 1 hour on my system..finally found the answer..Hope it would be useful to somebody else
created a new column,NewKPI and converted that to dt_str in dataconversion componet (as mentioned by Justin )
(DT_STR,3,1252)(LEN(NewKPI) <= 2 ? NewKPI : (SUBSTRING(NewKPI,3,1) == "1") ? "2A" : (SUBSTRING(NewKPI,3,1) == "2") ? "2b" : (SUBSTRING(NewKPI,3,1) == "3") ? "2c" : (SUBSTRING(NewKPI,3,1) == "4") ? "2d" : NULL(DT_WSTR,3))
(I have used substring instead of Left)
Thanks all for the help

Resources