Need an IF-ELSE inside a CASE-WHEN choice - tsql

I've got an existing stored procedure that looks like this:
SELECT TOP 1
...
CASE PX.Client.value('(Type)[1]', 'varchar(25)')
WHEN 'Sole Proprietor' THEN 'SPR'
WHEN 'Partnership' THEN 'PAR'
WHEN 'Corp (C)' THEN 'COR'
WHEN 'Corp (S)' THEN 'SCO'
WHEN 'LimitedCorp' THEN 'LLC'
WHEN 'Trust' THEN 'TRU'
END AS MyStructure,
...
INTO #Prod
FROM
#XMLIN.nodes('/MyDataset/MyRegistration') AS PX(Client)
Everything works great except I need to change the logic. Right now if Type is "LimitedCorp" then MyStructure is always "LLC". But I need to change it so that it sub-divides MyStructure based on the TaxClassification like so (pseudo-code):
...
WHEN 'LimitedCorp' THEN
IF PX.Client.value('(TaxClassification)[1]') == 'Partnership' THEN 'LP'
ELSE IF PX.Client.value('(TaxClassification)[1]') == 'SCorp' THEN 'LLS'
ELSE 'LLC'
WHEN 'Trust' ...
In my .NET code this would be a piece of cake! I'm not very strong on my stored procedure syntax though! Thanks!

Nest another case:
WHEN 'LimitedCorp' THEN
case
when PX.Client.value('(TaxClassification)[1]') == 'Partnership' then 'LP'
when PX.Client.value('(TaxClassification)[1]') == 'SCorp' then 'LLS'
else 'LLC'
end
WHEN 'Trust' THEN 'TRU'

Related

What is the most efficient to run certain lines of code only when a boolean is true?

If I had code that looked like the following:
if (bool1) {
statement1
statement2
} else if (bool2) {
statement3
statement4
}
and I only want to run statement 2 and 4 given another boolean (say bool3) is true, what is the best way to format that. I understand that I could add a nested if statement, but that seems bad from a maintainability perspective if I have 5 or more else ifs.
Any suggestions?
A nested if sounds perfectly fine to me at least. If none of the statements mess with bool1, bool3 and bool4 though and you really don't want to use nested ifs you could put them after eachother with conjunctions:
if (bool1) {
statement1
}
if (bool1 && bool3) {
statement2
}
if (bool2 && !bool1) {
statement3
}
if (bool2 && !bool1 && bool3) {
statement4
}
This looks terrible though so I'd just go with a plain and simple nested if.

Why does my ternary operator give a different result to an if else?

The problem I am having is I am trying to use ternary operators over if else for smaller code but when using it in my case it is returning a different result than if I use an if else.
The problem code is below;
If Else
if(string.IsNullOrEmpty(jn["LARM"].Value))
pm.ItemLeftArm = null;
else
pm.ItemLeftArm = jn["LARM"];
Ternary
pm.ItemLeftArm = string.IsNullOrEmpty(jn["LARM"].Value) ? null : jn["LARM"];
The jn["LARM"] is a json node from simpleJSON and it is either a number e.g. "0" or nothing e.g. "".
It returns null form the if else but it returns the jn object which transforms from "" into 0.
Im not sure why Im getting this issue.
The code is ok, the problem must be in the JSON. Have you tried logging the jn["LARM"].Value just before the terniary operator in order to be sure that the value is null/empty or not?
BTW, why are you using simpleJSON instead of the new Unity integrated JsonUtility?

What's wrong with this Factory Girl definition

#test/factories.rb
Factory.define :estado do |estado|
estado.nombre "Distrito Federal"
end
Factory.define :municipio do |municipio|
municipio.nombre "Cuauhtémoc"
municipio.estado { |estado| estado.association(:estado) }
end
Factory.define :colonia do |colonia|
colonia.nombre "Condesa"
colonia.municipio { |municipio| municipio.association(:municipio) }
end
#test/units/user_test.rb
test "Whats wrong with this"
assert(Factory.create(:colonia).id != 0)
end
The test fails miserably because Factory.create(:colonia) ALWAYS create a Colonia object with and id equals 0!!! #_#
Why is this factory creating objects with id 0?
It looks like a problem with the structure of your DB to me.
Is the id column in your 'colonias' table set to autoincrement?

Scala: concise way to express the following construct

I'll give some C-style "bracket" pseudo-code to show what I'd like to express in another way:
for (int i = 0; i < n; i++) {
if (i == 3 || i == 5 || i == 982) {
assertTrue( isCromulent(i) );
} else {
assertFalse( isCromulent(i) );
}
}
The for loop is not very important, that is not the point of my question: I'd like to know how I could rewrite what is inside the loop using Scala.
My goal is not to have the shortest code possible: it's because I'd like to understand what kind of manipulation can be done on method names (?) in Scala.
Can you do something like the following in Scala (following is still some kind of pseudo-code, not Scala code):
assert((i==3 || i==5 || i==982)?True:False)(isCromulent(i))
Or even something like this:
assertTrue( ((i==3 || i==5 || i==982) ? : ! ) isCromulent(i) )
Basically I'd like to know if the result of the test (i==3 || i==5 || i==982) can be used to dispatch between two methods or to add a "not" before an expression.
I don't know if it makes sense so please be kind (look my profile) :)
While pelotom's solution is much better for this case, you can also do this (which is a bit closer to what you asked originally):
(if (i==3||i==5||i==982) assertTrue else assertFalse)(isCromulent(i))
Constructing names dynamically can be done via reflection, but this certainly won't be concise.
assertTrue(isCromulent(i) == (i==3||i==5||i==982))
Within the Scala type system, it isn't possible to dynamically create a method name based on a condition.
But it isn't at all necessary in this case.
val condition = i == 3 || i == 5 || i == 982
assertEquals(condition, isCromulent(i))
I hope nobody minds this response, which is an aside rather than a direct answer.
I found the question and the answers so far very interesting and spent a while looking for a pattern matching based alternative.
The following is an attempt to generalise on this (very specific) category of testing:
class MatchSet(s: Set[Int]) {def unapply(i: Int) = s.contains(i)}
object MatchSet {def apply(s: Int*) = new MatchSet(Set(s:_*))}
val cromulentSet = MatchSet(3, 5, 982)
0 until n foreach {
case i # cromulentSet() => assertTrue(isCromulent(i))
case i => assertFalse(isCromulent(i))
}
The idea is to create ranges of values contained in MatchSet instances rather than use explicit matches.

Can I use SQLParameter when having a variable quantity of fields to update?

I have a table with eighty fields, none to seventy of them can change depending of an update process I have. For example:
if (process.result == 1)
cmd.CommandText = "UPDATE T SET f1=1, f6='S'" ;
else if (Process.result == 2)
cmd.CommandText = string.Format("UPDATE T SET f1=2, f12={0},f70='{1}'", getData(), st);
else if ..... etc.
I can optimize the building process of the UPDATE statement, however I would like to use SQLParameter; is that possible and convenient given the variablity of data to update?
Thanks.
For each if statement you currently are using inline string formats, you could just as well just add the sql params instead.
The formatting of the UPDATE string could be replaced with the selected sql params you require to be updated insted.