Nested IF ELSE in a derived column - sql-server

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

Related

Mule 4 Insert Date field into Oracle if it is present

I am trying to insert some data into an Oracle database and am using Mule 4 .
This is working with the current payload :
{
"empId" : "001",
"empName" : "Xyz"
}
The db insert :
<db:insert config-ref="Database_Config" target="insertEmp">
<db:sql><![CDATA[#[
"INSERT INTO EMP(EMP_ID,EMP_NAME) VALUES (:emp_id,:emp_name)" ]]]></db:sql>
<db:input-parameters>
<![CDATA[#[{emp_id: payload.empId,emp_name: payload.empName}]]]></db:input-parameters>
</db:insert>
Now the problem is the payload will contain empStartDate,empEndDate,createdDate
{
"empId" : "001",
"empName" : "Xyz",
"empStartDate" : "2019-07-17",
"empEndDate" : null , // can be null
"createdDate" : null // can be null
}
Now to insert into Oracle will need to convert empStartdate into a Date so I was thinking of using oracle Date function :
TO_DATE('2019-07-17', 'YYYY-MM-DD')
However I am not sure how to include this into my Mule flow ? Similarly in case of empEndDate if null should simply pass NULL while in case of createdDate I need to pass Oracle sysdate
Just not sure how to build the values dynamically and pass to Database ...

CAST string as DATETIME

I have the following query :
SELECT '31/12/1999 00:00:00' AS ODSUpdatedate
I want to ass a derived column ODSUpdatedate like below :
I am getting this following error :
Change the expression to :
(DT_DATE) "31/12/1999 00:00:00"

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.

Case or if statement in SSIS expression

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

How to count multiple fields with group by another field in solr

I have solr document which is like below.
agentId : 100
emailDeliveredDate : 2018-02-08,
emailSentDate : 2018-02-07
agentId : 100
emailSentDate : 2018-02-06
agentId : 101
emailDeliveredDate : 2018-02-08,
emailSentDate : 2018-02-07
I need a result like below.
agentId : 100
emailDeliveredDate : 1,
emailSentDate : 2
agentId : 101
emailDeliveredDate : 1,
emailSentDate : 1
In mysql it will be :
select count(emailDeliveredDate),count(emailSentDate) group by agentId;
I need help in solr for this.
I did not get any way in Solr which can help me. So I used facet with pivot which gave me half results. Rest half calculation I did in Java.

Resources