How could I go about writing the case statement as an SSRS expression so I don't have to modify the SQL source code? I don't understand how to call the field if the conditions are not met,
case when [Location Type] = 'COMMER' and [Contract] = '2)FRA Components' then 'Commercial Units'
when [Location Type] != 'COMMER' and [Contract] = '2)FRA Components' then 'NOn Commercial Units' else [Component Description] end [Component Description]
Any guidance appreciated
SDC's answer should be good but my preference, especially when nesting a couple of IIFs is to use SWITCH() instead.
It looks more like a SQL CASE statement too. Then final True acts like an ELSE.
=SWITCH(
Fields!Location_Type.Value = 'COMMER' and Fields!ContractValue = '2)FRA Components' , 'Commercial Units' ,
Fields!Location_Type.Value <> 'COMMER' and Fields!ContractValue = '2)FRA Components' , 'Non Commercial Units' ,
True, Fields!Component_Description.Value
)
The syntax is basically
=SWITCH(
Test1, ReturnValue1,
Test2, ReturnValue2,
Test3, ReturnValue3,
....
Test999, ReturnValue999
)
SWITCH tests each condition and returns the first that evaluates to True.
So by setting the final expression evaluated to True, if all previous expressions return false then this one will return a value.
A nested if expression should do the trick. I haven't had a chance to test it, but it should look something like this:
=IIF((Fields!Location_Type.Value = "COMMER") AND (Fields!Contract.Value = "2)FRA Components"), "Commercial Units", IIF((Fields!Location_Type.Value <> "COMMER") AND (Fields!Contract.Value = "2)FRA Components"), "Non Commercial", Fields!Component_Description.Value))
Related
Compilation error for nested iif
=IIf(Fields!no_of_employees.Value = max(Fields!no_of_employees.Value,"DataSet1"),"Lime","Gold",
IIf(Fields!no_of_employees.Value = min(Fields!no_of_employees.Value,"DataSet1"),"Red","Gold"
))
I was trying to implement nested iif.
If you look at your statement, you have passed 4 arguments to the first IIF
To write a normal IIF you would do something like
=IIF(
ConditionA=True,
TrueResult,
FalseResult
)
When writing a nested IIF you would do something like this
=IIF(
ConditionA=True,
TrueResult,
IIF(
ConditionB=True,
TrueResult,
FalseResult
)
)
To fix your code, remove the first instance of "Gold", .
I prefer to use SWITCH() instead of nested IIFs as I think it's easier to read.
If you want to use SWITCH() then you can rewrite your expression as follows.
=SWITCH(
Fields!no_of_employees.Value = max(Fields!no_of_employees.Value,"DataSet1"), "Lime",
Fields!no_of_employees.Value = min(Fields!no_of_employees.Value,"DataSet1"), "Red",
True, "Gold"
)
The final True just acts like an ELSE. You can read more about SWITCH() here .
https://learn.microsoft.com/en-us/sql/reporting-services/report-design/expression-examples-report-builder-and-ssrs?view=sql-server-ver16#DecisionFunctions
My expression is different than the ones I looked at already. MY code looks like this and I am having a hard time logically reading it. The code works but is part of another larger IIF statement that is used to filter out certain values on a given table column on the report.
I want to be able to "learn" how to read this type of IIF statement as it really does not make sense to me as written.
=IIF(Fields!DMDeptARCFlag.Value=1,
IIF(Fields!CloseFlag.Value=1,"",
IIF(Fields!ARCPaymentRequested.Value=0,"Invalid ARC Payment","")),"")
This expression is called "InvalidPaymentLoaded" and is used in the following expression
=TRIM
(IIF(IsNothing(Reportitems!WTBTIClosure.Value)=0 AND Fields!SrMgmtStatus.Value = 1 AND Fields!DMDeptARCFlag.Value = 1, Reportitems!WTBTIClosure.Value, "") +
IIF(IsNothing(Reportitems!ARCPaymentNotLoaded.Value)=0 AND Fields!DMDeptARCFlag.Value <> 1, Reportitems!ARCPaymentNotLoaded.Value, "") +
IIF(IsNothing(Reportitems!InvalidARCClose.Value) =0, Reportitems!InvalidARCClose.Value , "") +
IIF(IsNothing(Reportitems!BCDPayementNotLoaded.Value)=0, Reportitems!BCDPayementNotLoaded.Value, "") +
IIF(IsNothing(Reportitems!InvalidPaymentLoaded.Value)=0, Reportitems!InvalidPaymentLoaded.Value, "") +
IIF(IsNothing(Reportitems!InvalidMISCClosure.Value)=0, Reportitems!InvalidMISCClosure.Value, "") +
IIF(IsNothing(Reportitems!InvalidMCFClose.Value) =0, Reportitems!InvalidMCFClose.Value , "") +
IIF(IsNothing(Reportitems!InvalidDeductionClose.Value)=0, Reportitems!InvalidDeductionClose.Value, ""))
Your IIF is not that complex if you break it down....
Remember basic syntax for IIF is...
IIF([This is true], [then this], [else this])
So in you expression you have
"If DMDeptARCFlag =1 then do some more work, else return ""
=IIF(
Fields!DMDeptARCFlag.Value=1,
IIF(Fields!CloseFlag.Value=1,"", IIF(Fields!ARCPaymentRequested.Value=0,"Invalid ARC Payment",""))
,"")
If DMDeptARCFlag does equal 1 then we look at the first nested IIF which reads
If CloseFlag = 1 return "", else do some more work
IIF(Fields!CloseFlag.Value=1,"", IIF(Fields!ARCPaymentRequested.Value=0,"Invalid ARC Payment",""))
So if CloseFlag does not equal 1 then we look at the final nested IIF which simple reads
If ARCPaymentRequested = 0 return "Invalid ARC Payment", if not return ""
I think you could simplify the expression though as really you only ever return 1 of 2 values so if you work out all the conditions for returning one value you should be able to get rid of all those nested IIFs
Something like this
=IIF(
Fields!DMDeptARCFlag.Value=1 AND Fields!CloseFlag.Value <> 1 AND Fields!ARCPaymentRequested.Value=0
, "Invalid ARC Payment"
, "")
i designed a tablix report, i have a text box called Student-Attendance which dispaly the information below.
Student_Attendance
Sick
Absence
Present
I have tried to use IIF statement in order to show it as S,A,P. Other than "IIF" is there anything i could use in order to get my result.
IIF (Fields!Student_Attendance.value = "Sick", "S" ) and
IIF(Fields!Student_Attendance.value = "Absence" , "A")
IIF Takes 3 arguments
The condition (if field = value)
What to return if true (e.g. "S")
What to return if false - you are missing this
If you want to use IIF then you have to nest the IIFs
=IIF(Fields!Student_Attendance.value = "Sick", "S", IIF(Fields!Student_Attendance.value = "Absence" , "A") )
What might be simpler is SWITCH especially if you have more than a few options, something like this
=SWITCH (
Fields!Student_Attendance.value = "Sick", "S",
Fields!Student_Attendance.value = "Absence", "A",
Fields!Student_Attendance.value = "Present", "P"
)
I'm working on my colleague's old project and I found on her code WHERE TRUE AND ID='1' AND STAT='1'.
I've tried to removed TRUE AND so the query become WHERE ID='1' AND STAT='1' and get the same result.
I know we can use TRUEas boolean to search 'where something is true' such as WHERE FLAG = TRUE and this MySQL documentation state that
The constants TRUE and FALSE evaluate to 1 and 0, respectively. The constant names can be written in any lettercase.
I also tried SELECT * FROM MYTABLE WHERE TRUE but it's just the same as SELECT * FROM MYTABLE
what is the purpose of TRUE in her query?
It has no specific functional purpose. Your colleague may have included it if they were adhering to a specific style guide that recommends that all SELECT queries have explicit WHERE clauses. If an explicit WHERE clause is not provided, the default is to select all rows. Adding a WHERE clause that is always true will have the same effect.
Another way this query could have come about is if it was generated by a code generation tool that always had to write in a WHERE clause due to the way it was written.
for example:
myQuery = "SELECT X FROM Y WHERE " + ConditionMet(data)?" AccountID = '" + AcctId + "'" : "1=1";
This means that if ConditionMet(data) is true, then only return rows where AccountID matches the AcctId you are passing in. If it is false, then return all rows.
Adding a "dummy" 1=1 makes the code generator simpler to write. More generally, 1=1 is as legitimate a boolean clause as any other, and can be "dropped" into a conditional expression without having to special-case the query to omit the WHERE clause.
Similarly, adding a WHERE clause that is always false (e.g. "WHERE 1=0") will result in zero rows being returned.
Do note that the example code here is vulnerable to SQL Injection, so it should not be used in cases where you are dealing with AccountID's that you did not produce yourself. There are multiple ways to secure it that are beyond the scope of this answer.
If you're writing your SQLString on runtime, and you might add different "where" clausules but you don't know which of all of them will be the first, it makes it easy as all of them may start with "AND ....."
Example:
SQLString:='SELECT * FROM YOUTABLE WHERE TRUE'
If condition1 THEN SQLString:=SQLString+' AND Whatever=whatever';
If condition2 THEN SQLString:=SQLString+' AND Whatever=whatever';
If condition3 THEN SQLString:=SQLString+' AND Whatever=whatever';
If condition4 THEN SQLString:=SQLString+' AND Whatever=whatever';
otherwhise, you should add the WHERE clause not on the first SQLString:= but on the first condition that happens to be true, which you don't know will it be a priori
it is not as much relevant but if find important where adding dynamic conditions,for example in php.
$condition_stmt="";
if ($start_date !="" && $end_date!="")
{
$condition_stmt="and nos.status_date between '".$start_date."' and '".$end_date."'";
}
else if ($start_date!="")
{
$condition_stmt="and nos.status_date >='".$start_date."'";
}
else
{
$condition_stmt="and nos.status_date <='".$end_date."'";
}
$sql="select * from table where true ".$condition_stmt=.";
I have a field with below expression:
=join(iif(Parameters!FUP_Letter.Value = 1, "Yes", "No"), ", ")
When the report is loaded the field only says '#Error' instead of 'Yes, No', 'Yes' or 'No'
When I remove the IIF clause from this expression the result is just '1, 0' or '1' or '0'
As I am still new to SSRS I don't really know what I did wrong or how to find what I did wrong so if anyone could tell me the error or help me on my way I would be very grateful.
Kind regards
I suspect the error is being caused by you trying to join one value to nothing.
The IIF function isn't recursive, so it will return for the first value that you return so in the case above it will exit after evaluating 1 and returning yes. The join function then try's to join 'yes' to nothing and therefore errors.
you could do something like:
=Join(Replace(Replace(Parameters!FUP_Letter.Value,"1","Yes"),"0","No"), ",")
Actual used:
=Replace(Replace(join(Parameters!FUP_Letter.Value, ", "),"1","Yes"),"0","No")