ssis derived column - check if date field is null - sql-server

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.

Related

DATE Conversion in Snowflake does not work within CASE statement

I am trying to achieve this
CASE
WHEN
(DIM_TEST."TEST START DATE" <> ''
OR DIM_TEST."EST START DATE" IS NOT NULL
OR DIM_TEST."EST START DATE" <> 'NULL')
THEN TO_VARCHAR(TO_DATE(DIM_TEST."TEST START DATE", 'YYYYMMDD'), 'YYYY-MM-DD')
ELSE DIM_TEST."TEST START DATE"
END AS "FINAL_DATE"
It does not work
too many arguments for function [TO_DATE(DIM_TEST."TEST START DATE", 'YYYY-MM-DD')] expected 1, got 2
Any suggestions?
Thank You
The error indicates that the source column data type is not string but rather DATE/TIMESTAMP
SELECT TO_DATE(CURRENT_DATE(), 'YYYYMMDD')
-- Error: too many arguments for function [TO_DATE(CURRENT_DATE(), 'YYYYMMDD')] expected 1, got 2 (line 1)
The TO_DATE allows to use second argument only if the first argument is string:
TO_DATE( <string_expr> [, <format> ] )
TO_DATE( <timestamp_expr> )
TO_DATE( '<integer>' )
TO_DATE( <variant_expr> )
The entire CASE expression is probably not correct as the column is not string, and there are comparison against string literals '' and 'NULL'.
Finally, the first part of case expression tries to return stringTO_VARCHAR but the second is an original column, thus the final data type will be the data type of the column.

How to write NULL IN SSIS expressions

Requirement:
Fullname = if type is "Y" then Name else NULL
I tried expression
type == "y" ? Name : "NULL"
The field came as NULL as a string but would need as IS NULL
Any help is much appreciated. Thanks in Advance

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

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

Updating an array and converting from varchar to integer

I'm updating an array by adding a varchar to an int4 array. The 'varchar' field is all numbers so I tried casting it to ::integer but it isn't working
update dwh.attr_lookup set kli_tree = array[target_909_kli::integer] || kli_tree
is giving me this error
ERROR: ERROR: invalid input syntax for integer: ""
Query = update
dwh.attr_lookup set kli_tree = array[target_909_kli::integer]
|| kli_tree
What is the proper way to do this?
You're trying to cast an empty string to an integer and that doesn't work:
=> select ''::int;
ERROR: invalid input syntax for integer: ""
LINE 1: select ''::int;
^
You'll have to decide what you want to do with empty strings. If you want to convert them to zeros, then something like this should work:
array[case when target_909_kli = '' then 0 else target_909_kli::integer end]
Obviously your varchar field is not all numbers. There would not be any double quotes in this case - as the error message informs us.
Try giving a full example if the error has not clear by now. Including table definition and sample values.

Resources