How to make an IF Statement in SSIS - sql

How can I create a condition in SSIS like below?
I know how to create it just in SQL Server
Example:
IIF(LEFT(MAT,1) = '"', NULL, REPLACE(MAT,'""',''))

«boolean_expression» ? «when_true» : «when_false»
LEN([Mobile]) == 9 ? ("0" + [Mobile]) : [Mobile]

SET MAT = LEFT(MAT,1) == '"' ? NULL : REPLACE(MAT,'""','');
IFF statement works condition-based assignment
The IIF() statement has the following format:
=IIF( Expression to evaluate,
what-to-do when the expression is true,
what-to-do when the expression is false )
Parameter1: It should be a Boolean Expression.
Paremeter2: This value will return when Expression is true.
Paremeter3: This value will return when Expression is false.

The syntax in SSIS is like this: [condition] : [value if true] ? [value if false] With that said, you could do...
(MAT,1)='' : NULL ? REPLACE(MAT,'""',''))

Related

Can Laravel automatically switch between column = ? and column IS NULL depending on value?

When building a complex SQL query for Laravel, using ? as placeholders for parameters is great. However when the value is null, the SQL syntax needs to be changed from = ? to IS NULL. Plus, since the number of parameters is one less, I need to pass a different array.
To get it to work, I have written it like this, but there must be a better way:
if ($cohortId === null) {
// sql should be: column IS NULL
$sqlCohortString = "IS NULL";
$params = [
Carbon::today()->subDays(90),
// no cohort id here
];
} else {
// sql should be: column = ?
$sqlCohortString = "= ?";
$params = [
Carbon::today()->subDays(90),
$cohortId
];
}
$query = "SELECT items.`name`,
snapshots.`value`,
snapshots.`taken_at`,
FROM snapshots
INNER JOIN (
SELECT MAX(id) AS id, item_id
FROM snapshots
WHERE `taken_at` > ?
AND snapshots.`cohort_id` $sqlCohortString
GROUP BY item_id
) latest
ON latest.`id` = snapshots.`id`
INNER JOIN items
ON items.`id` = snapshots.`item_id`
ORDER by media_items.`slug` ASC
";
$chartData = DB::select($query, $params);
My question is: does Laravel have a way to detect null values and replace ? more intelligently?
PS: The SQL is for a chart, so I need the single highest snapshot value for each item.
You can use ->when to create a conditional where clause:
$data = DB::table('table')
->when($cohortId === null, function ($query) {
return $query->whereNull('cohort_id');
}, function ($query) use ($cohortId) {
// the "use" keyword provides access to "outer" variables
return $query->where('cohort_id', '=', $cohortId);
})
->where('taken_at', '>', $someDate)
->toSql();

Fix "dynamic" query without throwing me the given error?

UPDATE dbo.Einkauf_Web_Upload
SET
${
updatedUpload.Menge !== null
? `Anzahl = ${`${updatedUpload.Menge}`},`
: null
},
${
updatedUpload.ENummer !== null
? `ENummer = ${`'${updatedUpload.ENummer}'`}`
: null
}
WHERE ...
This query is supposed to differentiate between updated values of the object updatedUpload which, initially, has all of its values set to null. If the value is not altered therefor not updated, the query must not affect the particular column. In its current state, the query throws this error:
Incorrect syntax near the keyword 'null'
And I know why; if you do not alter the Menge attribute, the query looks like this:
UPDATE dbo.Einkauf_Web_Upload
SET null, ENummer = "abc"
WHERE ..
Is there a workaround to this? I am using NodeJs as my backend and thought of trying to make the column references dynamic via a mapped array which contains only the altered columns of updatedUpload.
Will appreciate any help!
SET null, ... is invalid SQL syntax, you should skip the null at all. Furthermore, remember not to do the query if there is nothing to update as SET WHERE ... is also invalid syntax.
I would suggest something like:
let updates = [
updatedUpload.Menge !== null ? `Anzahl = ${`${updatedUpload.Menge}`}` : null,
updatedUpload.ENummer !== null ? `ENummer = ${`'${updatedUpload.ENummer}'`}` : null,
// ... add here
]
// Filter out null updates
updates = updates.filter(u => !!u);
// Do query only if updates are avaliable
if (updates.length > 0) {
const sql = `UPDATE dbo.Einkauf_Web_Upload SET ${updates.join(', ')} WHERE ...`;
// ... execute
}

How to write Case statement in SSIS expression

I have a small condition in SQL Code. How can I write that in SSIS in Derived column task.
CASE WHEN SaleProduct IS NULL AND ReProduct IS NULL THEN ''
WHEN SaleProduct IS NOT NULL AND ReProduct IS NULL THEN SaleProduct
WHEN SaleProduct IS NULL AND ReProduct IS NOT NULL THEN SaleProduct
ELSE
SaleProduct +';'+ ReProduct END As COL
I have tried with the following expression
but not getting anything
(ISNULL( [SaleProduct] )&& [ReProduct] == "") : ? "0"
" ------------------------"
UNKNOWN
can anyone help me on this ...
CASE statements can be replicated by using Conditional operator in the Derived column transformation.
For your condition the expression would be like
((ISNULL(SaleProduct) == True && ISNULL(ReProduct) == True) ? " " :((ISNULL(SaleProduct) == False && ISNULL(ReProduct) == True)? SaleProduct :(ISNULL(SaleProduct) == True && ISNULL(ReProduct) == False)? SaleProduct : SaleProduct+";"+ReProduct)
You need to use Ternary operator in Derived column transformation:
To understand and depict your condition:
( ISNULL([SaleProduct]) && ISNULL([ReProduct]) ) ? " " :
( !ISNULL([SaleProduct]) && ISNULL([ReProduct]) ) ? [SaleProduct] :
( ISNULL([SaleProduct]) && !ISNULL([ReProduct]) ) ? [SaleProduct] : ( [SaleProduct]+";"+[ReProduct] )
But tabs causes error in Derived column transformation. Hence it should actually be:
(ISNULL([SaleProduct]) && ISNULL([ReProduct])) ? " " : (!ISNULL([SaleProduct]) && ISNULL([ReProduct])) ? [SaleProduct] : (ISNULL([SaleProduct]) && !ISNULL([ReProduct])) ? [SaleProduct] : ([SaleProduct]+";"+[ReProduct])

How to get value of nested Map using parameter

I'm trying to get value of nested Map using parameter of SQL component expression but it failed.
I have this json (unmarshal to java.util.Map):
{ "username" : "john",
"company" : { "companycode" : "stackoverflow.inc",
"address" : "Street 12" }
}
And I have this SQL expression with parameters on my Route builder:
...
.unmarshal().json(JsonLibrary.Jackson)
.to("sql:INSERT INTO user_tab VALUES (:#username, :#company.companycode)")
...
I was able to get the value of username but I can not get the value of companycode. What is the right way to do it? Thank you.
according: http://camel.apache.org/sql-component.html
From Camel 2.14 onward you can use Simple expressions as parameters as shown:
sql:select * from table where id=:#${property.myId} order by name[?options]
this work fine for me:
{guid=67, properties={last_seen=1472034553348, _type=business_service, name=Anna, created_at=1472033602648, status=USUNIĘTY}}
<to uri="dbSQL:insert into table_name (
guid,name,description,type,status,changedate,transid,transseq
) values (
:#${body[guid]}::integer, :#${body[properties][name]}, :#${body[properties][name]}, :#${body[properties][_type]}, :#${body[properties][staus]}, now(), 0, 0
)"/>

sql query in Rails where value IS BLANK (0 or nil)

In Rails there is the method item.property.blank? which is true for property being "0" or nil or whitespace.
How can I make an sql query to check if a value is blank?
I now have:
Item.where("name = 'Testname' AND property <> '1'")
which shows me all items with name = 'Testname' and property = '0', but not those with property = nil (not defined).
How should my sql query be to be equivalent to
Item.where("name = 'Testname' AND property IS BLANK")
so that all items with property = '0' and property = nil are included to the search results?
Item.where("property IN ('0','') OR property IS NULL")
or more exactly...
Item.where("name = 'Testname' AND (property IN ('0','') OR property IS NULL)")
Without using SQL strings, you can do this:
Item.where(name: 'Testname', property: [nil, '0', ''])
It will produce the SQL query
SELECT `items`.* FROM `items`
WHERE `items`.`name` = 'Testname'
AND (`items`.`property` IN ('0', '') OR `items`.`property` IS NULL)