witing a case statement in SSIS - sql

I'm setting up a package to import an excel spreadsheet into a SQL database. There is a column in the spreadsheet where I would like to pick out keywords and then put them in a new column. In SQL it would be like a basic case statement
case when column_A like '%Norwich%' then 'Norwich'
when column_A like '%Ipswich%' then 'Ipswich'
when column_A like '%Cambridge%' then 'Cambridge'
else 'NA'
end as NewColumn
I have tried the below but I'm guessing its not working properly because I have now wildcards
[Report Title] == "Norwich" ? "Norwich" : [Report Title] == "Ipswich" ? "Ipswich" : [Report Title] == "Cambridge" ? "Cambridge" : "NA"
Example:
Report Title NewColumn
Norwich is in Norfolk Norwich
Cambridge is in Cambridgeshire Cambridge
Suffolk is home to Ipswich Ipswich

You have to use FINDSTRING() function with nested conditional operators to achieve that:
FINDSTRING([Report Title],"Norwich",1) > 0 ? "Norwich" : (
FINDSTRING([Report Title],"Ipswich",1) > 0 ? "Ipswich" : (
FINDSTRING([Report Title],"Cambridge",1) > 0 ? "Cambridge" : "NA"))
References
Nested Conditional Operators in an SSIS Derived Column
FINDSTRING (SSIS Expression)

Related

SSIS expression similar to case statement

I need to write an expression for derived column. My column name is 'status'. what is the equivalent expression in SSIS for the below condition?
Case when Status Like '%Open%' then 0
when Status like '%Won%' then 1
when status like "%Lost%' then 2 Else 3
Thanks in advance
Give this a shot:
FINDSTRING(Status,"Open",1) > 0 ? 0 : (FINDSTRING(Status,"Won",1) > 0 ? 1 : (FINDSTRING(Status,"Lost",1) > 0 ? 2 : 3))

How to use "case-when" in Ecto Queries in elixir?

I have an SQL query like :
SELECT SUM(CASE WHEN <table_name>.status = '2' THEN 1 ELSE 0 END) FROM <table name>.
I want to write the corresponding Ecto Query for the above. Something like:
from t in <table_name>, select: sum(...)
What is the analogy to "case-when" in the above case?
Like the comment said, you can use fragment/1:
query = from t in <Model>, select: fragment("SUM(CASE WHEN status = ? THEN 1 ELSE 0 END)", 2)
If you want to specify the table, this works for me:
query = from t in <Model>, select: fragment("SUM(CASE WHEN ? = ? THEN 1 ELSE 0 END)", t.status, 2)
You can also leverage on macros to extend Ecto query language:
defmacro case_when(condition, do: then_expr, else: else_expr) do
quote do
fragment(
"CASE WHEN ? THEN ? ELSE ? END",
unquote(condition),
unquote(then_expr),
unquote(else_expr)
)
end
end
Then use it like this:
query = from t in <Model>,
select: case_when t.status == 2
do 1
else 0
end

conditional column mapping in ssis

I am a newbie on SSIS. I have a SOURCE table with columns s.CASH , s.ACC_ID and s.ADDITIONAL_NUM and a TARGET table with column t.ACCT_NUM in my SSIS package. Here is the mapping logic -
If s.CASH > 0 , map s.ACC_ID to t.ACCT_NUM
else map s.ADDITIONAL_NUM to t.ACCT_NUM.
If s.ADDITIONAL_NUM is empty, then t.ACCT_NUM = null
How can implement it in SSIS?
#billinkc - Thanks for your suggestion. I chose to create a single derived column and applied following condition -
(CASH > 0) ? [ACC_ID] : ([ADDITIONAL_NM]=="" ? ( DT_WSTR,255) NULL(DT_WSTR,255) : [ADDITIONAL_NM])

How to change variable value on demand in JasperReports

I am working with iReport. Here is the situation:
I have some parameters named branchID, spType.
I have created one variable named branchName (string) which is by default null.
What I need to do is, to change/ edit the value of the variable branchName depending on the values of branchID and spType
How can I do this with JasperReports using If Else. Is it something with the properties of the variable branchName? Any help/ Suggestion would be highly appreciated.
Expressions Scripting Language in Jasper reports is your friend!
What i can fathom from your query is that you want to compute the value of a variable based on values of two parameters. In short, you want to do something like:-
Simple if-else
If (branchId == 1 && spType == "abc")
branchName = "Xyz1";
else
branchName = "Xyz2";
In Jasper scripting language, the above expression can be written as :-
($P{branchId}==1 &&
$P{spType} != null &&
$P{spType}.equals("abc"))?"Xyz1":"Xyz2"
Nested If-else
If (branchId == 1 && spType == "abc")
branchName = "Xyz1";
else if (branchId = 2 && spType == "def")
branchName = "Xyz2";
else
branchName = "Xyz3";
Jasper expression:
( $P{branchId} ==1 &&
$P{spType} !=null &&
$P{spType}.equals("abc") ) ? "Xyz1" :
(($P{branchId} == 2 &&
$P{spType} != null &&
$P{spType}.equals("def")) ? "Xyz2" : "Xyz3");
For more details, have a look at this tutorial:
http://www.tutorialspoint.com/jasper_reports/jasper_report_expression.htm
Here is a similar stackoverflow question also:
doing comparison if else in JasperReports
For core concept, Page no. 10 of this pdf:
http://jasperreports.sourceforge.net/JasperReports-Ultimate-Guide-3.pdf
iReport support scripting using expressions.
You can right click on a report element and click edit expression to open the expression editor.
Some useful notes in this answer as well.

Conditional Where clauses in JasperReports

Let's say I want a JasperReport that lets the user filter on a date if they so wish. The SQL is as follows:
select * from foo where bar = $P{bar} and some_date > $P{some.date}
Now, I don't want to filter by some date if they didn't pass the date in. I found the following kludge that people use:
select * from foo where bar = $P{bar} $P!{some.date.fragment}
And the some.date.fragment parameter is defined with the following default:
($P{some.date} == null || $P{some.date}.equals("")) ? "" : "AND some_date >'" + new java.sql.Date($P{some.date}.getTime()).toString() + "'"
This is not working as the toString doesn't output the date in a format that my SQL server understands. I would like to have the conditional still use a prepared statement with the jdbc driver and toss the parameter in, I just want the prepared statement to be dependent on if the parameter is null or not. Can this be done?
Before you have used the $P!{} expression the JDBC-Driver does all formatting for you.
But if you use the $P!{} expression you have to format yourself.
Something like this should work:
(
$P{some.date} == null
?
""
:
"AND some_date >'" + (new SimpleDateFormat("dd.MM.yyyy HH:mm:ss.SSS")).format($P{some.date}) + "'"
)
Depending on your data type you have to customize dd.MM.yyyy HH:mm:ss.SSS.
If you don't want to use the $P!{} expression you can avoid it with the solution below.
I personally don't like this way. It also may cause a bad execution plan.
If don't want to use $P!{} because you worry about sql injection. It's needless as long your parameter $P{some.date} contains a safe data type like java.lang.Date.
Create a parameter. Let's call it ${is_null_pram} and add a default expression with param class Integer:
($P{some.date} == null ? 1 : 0)
Now you can query:
SELECT
*
FROM foo
WHERE
bar = $P{bar}
AND
(
some_date > $P{some.date}
OR 1 = $P{is_null_pram}
)
I think you can use the function:
$X{EQUAL, <column_name>, <parameter_name>}
It optimizes the query as you can see in this help page.
You can use this conditional statement
select *
from foo
where bar = $P{bar} and some_date > $P{some.date} or $P{some.date} is null