SSIS expression similar to case statement - sql

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

Related

SQL using where like "%ads%" + CASE WHEN

My query is
SELECT Abonent FROM phone
WHERE `Street` LIKE "%примiська%" AND WHERE
CASE WHEN `House` % 2 = 0 THEN 'Правий'
ELSE 'Лiвий'
END AS Side
And I get
How can I do that case ?
The second where is definitely incorrect; it seems you want case in select: you filter Abonent being on required Street(s) and add a computed field to select showing on which side of a street (left or right) each abonent is.
select Abonent,
case
when `House` % 2 = 0 then 'Правий'
else 'Лiвий'
end as Side
from phone
where `Street` like '%примiська%'

Spark Sql Parser append additional parameter in UDF call

I use SQL statements as input from users something like "CASE WHEN CALL_UDF("12G", 2) < 0 THEN 4 ELSE 5 END".
I would like to add in this string an additional parameter.
Expected result: "CASE WHEN CALL_UDF("12G", 2, additional_parameter) < 0 THEN 4 ELSE 5 END".
To achieve this goal I am trying to use SparkSqlParser but faced problems during the implementation of this replacement. Probably someone has implemented a similar solution. Thanks.
What I have already tried:
val expression = parser.parseExpression("CASE WHEN CALL_UDF("12G", 2) < 0 THEN 3 ELSE 4 END")
.transformDown {
case expression if expression.isInstanceOf[UnresolvedFunction] && expression.asInstanceOf[UnresolvedFunction].name.funcName == "CALL_UDF" =>
UnresolvedFunction(FunctionIdentifier("CALL_UDF"), expression.children.toList ++ Seq(parser.parseExpression("4")), false)
}

witing a case statement in SSIS

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)

What does this SQL statement produce?

I am having trouble finding documentation about this specific statement
SELECT IF (count(f.id)=0,1,0) as flgNew
FROM table f ON ...
WHERE ...
the table is joined with other tables and should return 1 if the entry in f is found or 0 if it has not been found.
So what does IF (count(f.id)=0,1,0) do?
It's basically the same as the following CASE statement:
SELECT CASE WHEN count(f.id) = 0
THEN 1
ELSE 0
END AS flgNew
...
It checks to see if the expression count(f.id) = 0 is true, and returns the value 1 if it is, and 0 if it is not.
You can read more on the IF() function in the official docs here:
https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html#function_if

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