CAST string as DATETIME - sql-server

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"

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 ...

Concatenate string using SSIS expression builder

I'm trying to concatenate some strings using expression builder within SSIS depending on a variable being null or not but get the following error message
"String1" + ISNULL( #[User::ReservingAnalysisClassCodes]) ? "String2" : "String3"
Per Nick from the comments, this should work:
"String1" + (ISNULL( #[User::ReservingAnalysisClassCodes]) ? "String2" : "String3")
SSIS is trying to concatenate "String1" + ISNULL( #[User::ReservingAnalysisClassCodes] which is why it's throwing an error.

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

Conversion of a varchar data type to a datetime data type resulted in an out-of-range value in TSQL

In my T-SQL select there appears to be an error with my datetime
select
t.F47, t.F53, t.F40, t.F162, t.F163,
N'10' as kostenart, t.F39, t.F2, t.F5,
convert(nvarchar, cast(t.F9 as datetime), 112),
t.PARID, 20170928135800 as exportzeitstempel
from
T_TRANS6 as t
where
t.F20 = N'Erledigt'
and t.F9 < convert(datetime, '01.09.2017 00:00:00', 104)
The error message is German and it says:
Meldung 242, Ebene 16, Status 3, Zeile 1
Bei der Konvertierung eines nvarchar-Datentyps in einen datetime-Datentyp liegt der Wert außerhalb des gültigen Bereichs.
I tried to translate this to:
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value."
I really don't know how I did from so thanks for any help guys
Your problem is triggered by cast(t.F9 as datetime).
Please do : SELECT getdate(); to get the implicit "datetime to string" convertion format.
WARNING : Implicit convertion format are set at the instance level. It can differ from a server to another, even in the same compagny...
This will gives you someting like dd.MM.yyyy HH:mm:ss or yyyy-MM-dd HH:mm:ss or ...
The given format is the one expected and required for all F9 of TRANS6 table records!!
A single TRANS6.F9 with a wrong formating patern will raise this ERROR. So analyse your F9 data, find the concerned rows, clean them and retry...
NOTE : CONVERT(NVARCHAR(8), getdate(),112) get a string like YYYYMMDD (Ex : '20170928') witch is the only sortable string format of dates...
EDIT : F9 = '2016.10.30' and Implicit convertion expect '2016-10-30' !!
Try this :
select
t.F47, t.F53, t.F40, t.F162, t.F163,
N'10' as kostenart, t.F39, t.F2, t.F5,
convert(nvarchar, convert(datetime,t.F9,102), 112),
t.PARID, 20170928135800 as exportzeitstempel
from
T_TRANS6 as t
where
t.F20 = N'Erledigt'
and t.F9 < convert(datetime, '01.09.2017 00:00:00', 104)
Does it works?

Resources