How to write NULL IN SSIS expressions - sql-server

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

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.

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

Simplifying a SQL Server query with a shortcut

I have a query where many columns could be blank or null. They actually have longer names than the example below which I am using as an example:
select *
from table1
where field1 is not null and field1 != '' and
field2 is not null and field2 != ''
...etc
It gets tiresome having to type
x is not null and x != ''.
Is there some way to specify "x is not null and x != ''"?
Like for Java with
StringUtils.isNotEmpty(x)
I use
where isnull(x, '') <> ''
a lot. I find it a bit easier to "understand" than nullif.
-- EDIT ---------------------------------------
I missed that they were all ANDed together. So, if all N fields must be non-null and not empty, assuming that all fields are strings (varchars), this should do it:
where isnull(field1 + field2 + field3 + ... + fieldN, '') <> ''
First, the strings are concatenated together:
If any are null, the result will be null
If none are null and all are empty, the result will be an empty string
Else, the result will be a non-empty string
Next, the results are isnulled:
If the concatenated value is null, it is set to an empty string
Else, you get the concatenated contents (empty or not-empty string)
Last, compare that with the empty string:
If True, then either all are empty or one or more is null
If False, none are null and at least one is not empty
Try
WHERE NULLIF(field1, '') IS NULL
For SQL Server, I would use COALESCE for this:
WHERE COALESCE(field1, '') > ''
ISNULL also works
If you want to exclude rows where every field is null or blank you can do it like this:
WHERE COAlESCE(Field1,Field2,Field3,Field4,Field5,'') <> ''

What does a null value from db2 look like when it is retrieved from C code?

Currently, I am able to retrieve values from IBM DB2. What I needed to be able to do is see what that value looks like if it is null.
In my code I set up a conditional statement that checks for most null values except I apparently do not know what a null value would like if it is retrieved from DB2 in my C code.
Currently I have:
if(pchr == NULL || pchr == "Null" ||
pchr == "NULL" || pchr == '\0' || pchr == “null”)
where pchr is a character pointer.
Apparently this conditional statement cannot detect the null value from DB2. Any help would be awesome
Null is usually indicated with a separate variable. For DB2 embedded SQL, it looks something like this:
EXEC SQL BEGIN DECLARE SECTION;
VARCHAR pchr[PCHR_LEN+1]; /* nullable field we're interested in */
short pchrInd; /* flag indicating field is currently null */
EXEC SQL END DECLARE SECTION;
...
EXEC SQL SELECT phcr INTO :pchr:pchrInd FROM table; // or SELECT pchr INTO :pchr INDICATOR :pchrInd
if ( pchrInd )
// pchr is null
else
// process pchr

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