IF statement for last value in LookupSet expression - arrays

I have an expression to get the last date value but I get '#Error' if there is no date entered. (IF part returns #Error because no date has been entered - the Else part returns a date). Here is the expression:
IIF(LookupSet(Fields!Denial_ID.Value,Fields!Denial_ID.Value,Fields!Appeal_Date_Entered.Value,"DataSet2").Length() =0, "", LookupSet(Fields!Denial_ID.Value, Fields!Denial_ID.Value, Fields!Appeal_Date_Entered.Value, "DataSet2")(LookupSet(Fields!Denial_ID.Value, Fields!Denial_ID.Value, Fields!Appeal_Date_Entered.Value, "DataSet2").Length() -1))

I'm definitely not a report writer, but there's some strange stuff in your expression which make me expect it not to work. Below is a formatted version of it (it's easier to analyse it, although I have no idea of whitespace syntax in MRB):
IIF(
LookupSet(Fields!Denial_ID.Value,
Fields!Denial_ID.Value,
Fields!Appeal_Date_Entered.Value,"DataSet2").Length() =0,
"",
LookupSet(Fields!Denial_ID.Value,
Fields!Denial_ID.Value,
Fields!Appeal_Date_Entered.Value, "DataSet2")
(LookupSet(Fields!Denial_ID.Value,
Fields!Denial_ID.Value,
Fields!Appeal_Date_Entered.Value,
"DataSet2").Length() -1)
)
According to docs, the IIF syntax is the following:
=IIF(condition, valueIfTrue, valueIfFalse)
but your expression looks like this:
IIF(condition, valueIfTrue, valueIfFalse (someOtherStuff))
so you should describe what you expect to get from that expression and change it to accord the standart syntax (see also examples on the referenced page).

Related

Converting Excel Formula involving *OR to TSQL

I'm trying to convert this formula into T-SQL.
=IF(D6>F6,D6-C6-1,F6-C6-1)*OR(IF(F6-C6-1<0,0,F6-C6-1))
Does anyone know what the *OR actually means?
The columns C, D, F are all Date fields.
For example
For the first line, this part of the formula IF(D6>F6,D6-C6-1,F6-C6-1) returns 35 and the second part of the formula IF(F6-C6-1<0,0,F6-C6-1) returns 35 as well. So shouldn't the result be 35 x 35 = 1225. The correct answer is 35 however.
As per my earlier comment:
The second parameter in the OR would be optional. As a matter of fact, or will just return 'TRUE' if any of it's paramaters is 'TRUE', no matter the amount of (optional) parameters you use. The 'OR' in this case just evaluates the outcome of the second 'IF' (which is 35). So > 'OR(35)' which evaluates to TRUE. Since you multiply this will again evaluate to '35*1' being your final result. The formula is looking strange because 'OR' would always return true in this case. The formula might as well just be '=IF(D6>F6,D6-C6-1,F6-C6-1)'

Compare and divide in report builder

I have a condition I want to divide two values in report builder with same date but different sample name in the same table...
For example, in this image, I want to divide the CM2(2.85) and Raw Meal(0.58) value. their result should be 4.9.
And if these two parameters (CM2 and Raw Meal) are not on the same date then the value should be empty or nothing. Please help I am new to report builder expression.
I've tried this expression but it does not give me what I need
IIf(InStr(Fields!Sample_Code.Value,"CM2") > 0, Fields!So3.Value, nothing) / IIf(InStr(Fields!Sample_Code.Value,"Raw Meal") > 0, Fields!So3.Value, nothing)
The record will either be CM2 or Raw Meal, but it will never contain both at the same time. If Fields!Sample_Code.Value = "CM2" is true, the second half of the expression will be false, or vice-versa. Fields!Sample_Code.Value can't be two different values at the same time, it can only contain the data from a single record.
Your expression will result in either:
nothing/Fields!So3.Value
or
Fields!So3.Value/nothing.
As a simplified example:
IF( x=1 , 1 , null) / IF( x=2 , 1 , null)
X cannot be two different values at the same time so the expression will never return a non-null result.
You'll need to join CM2 and RAW MEAL records together to evaluate them at the same time. That would probably require a significant change to what you've posted so far.

Solr FunctionQuery: gte, lte, eq functions

Solr FunctionQuery has a DIV(x,y) function. I have such a need if y=0, then y should be equal to x.
In other words, I need to represent the following logic with FunctionQuery:
if y == 0, return 1 /* i.e. DIV(x,x) */
else, return DIV(x,y)
Somehow, from the Solr doc, I cannot find any comparison function, e.g. EQ(x, value), etc. for me to use.
Will anyone be able to give me a hint to construct my desired logic using FunctionQuery?
Thanks!
To clean up this question and log what is my final solution, thanks to Srikanth Venugopalan comment:
actually you need to switch the arguments. exposure_count = 0 is interpreted as false. So your condition would be {!boost b=if(exposure_count,div(1,exposure_count),1)}"
As it seems, Lucid works documentation does have a mistake. The FunctionQuery parser does not take comparison operators such as ==, at least this is what I found by looking into the sourcecode. Also, the field separator for IF() function should be ,(comma) and not ;(semi-colon).
The official Solr wiki is correct.
For string terms this works:
if(termfreq(fieldname,"value"),2,1)
which yields 2 if "value" is contained in "fieldname" (termfreq will be >0)
you can use == for equals and if conditional statement as below :-
e.g. if(y == 0; 1; DIV(x,y))
Check for the example # Documentation
if(color=="red"; 100; if(color=="green"; 50; 25)) :
This function checks the document field "color", and if it is "red" returns 100, if it is "green" returns 50, else returns 25.
Edit :-
Solr if wiki mentions , (comma) as seperator
e.g. if(exists(myField),100,0)

SSIS How to get part of a string by separator

I need an SSIS expression to get the left part of a string before the separator, and then put the new string in a new column. I checked in derived column, it seems no such expressions. Substring could only return string part with fixed length.
For example, with separator string - :
Art-Reading Should return Art
Art-Writing Should return Art
Science-chemistry Should return Science
P.S.
I knew this could be done in MySQL with SUBSTRING_INDEX(), but I'm looking for an equivalent in SSIS, or at least in SQL Server
Better late than never, but I wanted to do this too and found this.
TOKEN(character_expression, delimiter_string, occurrence)
TOKEN("a little white dog"," ",2)
returns little the source is below
http://technet.microsoft.com/en-us/library/hh213216.aspx
of course you can:
just configure your derived columns like this:
Here is the expression to make your life easier:
SUBSTRING(name,1,FINDSTRING(name,"-",1) - 1)
FYI, the second "1" means to get the first occurrence of the string "-"
EDIT:
expression to deal with string without "-"
FINDSTRING(name,"-",1) != 0 ? (SUBSTRING(name,1,FINDSTRING(name,"-",1) - 1)) : name
You can specify the length to copy in the SUBSTRING function and check for the location of the dash using CHARINDEX
SELECT SUBSTRING(#sString, 1, CHARINDEX('-',#sString) - 1)
For the SSIS expression it is pretty much the same code:
SUBSTRING(#[User::String], 1, FINDSTRING(#[User::String], "-", 1)-1)
if SUBSTRING length param returns -1 then it results in error,
"The length -1 is not valid for function "SUBSTRING". The length parameter cannot be negative. Change the length parameter to zero or a positive value."

Is SQL Server's double checking needed here?

IF #insertedValue IS NOT NULL AND #insertedValue > 0
This logic is in a trigger.
The value comes from a deleted or inserted row (doesn't matter).
2 questions :
Do I need to check both conditions? (I want all value > 0, value in db can be nullable)
Does SQL Server check the expression in the order I wrote it ?
1) Actually, no, since if the #insertedValue is NULL, the expression #insertedValue > 0 will evaulate to false. (Actually, as Martin Smith points out in his comment, it will evaluate to a special value "unknown", which when forced to a Boolean result on its own collapses to false - examples: unknown AND true = unknown which is forced to false, unknown OR true = true.) But you're relying on comparison behaviour with NULL values. A single step equivalent method, BTW, would be:
IF ISNULL(#insertedValue, 0) > 0
IMHO, you're better sticking with the explicit NULL check for clarity if nothing else.
2) Since the query will be optimised before execution, there is absolutely no guarantee of order of execution or short circuiting of the AND operator.
Combining the two - if the double check is truly unnecessary, then it will probably be optimised out before execution anyway, but your SQL code will be more maintainable in my view if you make this explicit.
You can use COALESCE => Returns the first nonnull expression among its arguments.
Now you can make the query more flexible, by increasing the column limits and again you need to check the Greater Then Zero condition. Important point to note down here is you have the option to check values in multiple columns.
declare #val int
set #val = COALESCE( null, 1, 10 )
if(#val>0)
select 'fine'
else
select 'not fine'

Resources