I am attempting to write a nested iif statement in SSRS that will convert the varchar values that are numbers(isnumeric) to integer and leave the text as is. I also want to be sure if the value in the cell is "0" it will return a blank or "". Please see the code I am using below. Any insights as to why the non-numeric text is showing as #Error would be greatly appreciated. Thanks!
=iif(
Fields!O1_Parent_Line_Item_ID.Value is "0", "", iif(ISNUMERIC(Fields!O1_Parent_Line_Item_ID.Value), CInt(Fields!O1_Parent_Line_Item_ID.Value), Fields!O1_Parent_Line_Item_ID.Value))
Use a SWITCH statement instead of nested IIFs and test for non numeric first. SWITCH statements stop at the first expression that returns true.
=SWITCH (
ISNUMERIC((Fields!O1_Parent_Line_Item_ID.Value), CInt(Fields!O1_Parent_Line_Item_ID.Value),
Fields!O1_Parent_Line_Item_ID.Value = "0", ""
True, Fields!O1_Parent_Line_Item_ID.Value
)
The above statement will test each expression in turn until it finds one that returns true. So if it's numeric, convert to int. If it's "0" return and empty string. If both those return false then the last expression will always return true so you will get you original value.
Note that 0 numeric values will return 0 not "" as they are obviously numeric so the first expression will catch it.
Related
I am working on an add-query.
In the add query I am trying to fill field C with with a true or false value, depending on the value in field A.
If the value in field A equals -1 the value in field C should be true (-1)
I thought that the solution would be something like the following, but I am getting #Error in the results:
C: IIf([A]='-1',True,False)
A solution that seems to return the desired outcome is the following:
B: IIf(Nz([A])='-1',True,False)
The problem with this (NZ-function) is that it throws an error when running the query with VB (ADO,DAO or OLEDB)
My question is:
What formula can be used to get to the desired results without using the NZ-function
The desired results are as given in field B
Since you're using quotes, I assume your value is a string. In that case, Nz will convert a Null to a zero-length string.
You can achieve the same by simply concatenating an empty string:
IIf([A] & ''='-1',True,False)
A more general solution is to use IIf, which allows you to specify an alternative value on nulls:
IIf(IIf([A] IS NULL, '', [A]) = '-1', True, False)
As [A] is numeric, all you need is to compare it with True:
C: [A]=True
CASE
WHEN code IN ('FJS354', 'JDF334')
THEN 'Lower_form'
ELSE 0
END AS format
This returns an error in Redshift
invalid input syntax for integer: "Lower_form"
I know if I change 'Lower_form' to an integer it will work however I want this column to be a string. Is there a way to do this?
I want this column to be a string.
All branches of a case expression must return the same datatype. You are giving two literal values whose datatype is not the same (string vs integer): the database makes the decision to turn them both to integers - which is not what you want.
Rremove the ambiguity by being explicit about the datatype you want to return. That is, make this literal 0 a string:
CASE WHEN code in ('FJS354','JDF334')
THEN 'Lower_form'
ELSE '0'
END as format
Im wondering if some can help we understand why SSRS returns Error in the my Textbox13 text box.
Warning 1 [rsRuntimeErrorInExpression] The Value expression for the textrun ‘Textbox13.Paragraphs[0].TextRuns[0]’ contains an error: Conversion from string "NULL" to type 'Date' is not valid.
The value Workorder_Closed_Date is a result of a sql query, and is either filled in or NULL
=IIF(IsNothing(Fields!Workorder_Closed_Date.Value),
reportitems!Textbox13.Value="open",
reportitems!Textbox13.Value="closed")
If that expression is for Textbox13, then this would work:
=IIf(IsNothing(Fields!WorkOrder_Closed_Date.Value), "open", "closed")
Note that IIf executes both true and false parts, so you want a constant expression in each branch of the condition, not side-effects.
=IIf(condition, value_if_true, value_if_false)
Oh god.
Warning 1 [rsRuntimeErrorInExpression] The Value expression for the textrun ‘Textbox13.Paragraphs[0].TextRuns[0]’ contains an error: Conversion from string "NULL" to type 'Date' is not valid
Your value isn't null (or Nothing actually) - it's a STRING literal that contains the VALUE "NULL".
You need to fix your data, not your report.
I want to have an If condition based on a condition obtained from a database. So each row in this table has a condition as a string on when something should be displayed.
Example: the row has the column "DisplayWhen" with the value "Status="M"".
The syntax of the condition statement is vb.net and the variable "Status" is available in the code.
So my statement is:
If CBool(rdr.Item("DisplayWhen").tostring.trim) then
'something
End If
But I keep getting the error: Conversion from string "Status="M"" to type Boolean is not valid.
Any help would be appreciated.
As I said in my comment a string in itself can not evaluated to a Boolean, you need to compare it to your expected value. Something like this.
If CBool(rdr.Item("DisplayWhen").ToString.Trim = "Status=""M""") Then
'something
End If
or using the Equals Method
If rdr.Item("DisplayWhen").Equals("Status=""M""") Then
'something
End If
What I'm trying to do is the following: I have 3 columns (Control_OpenDate, Control_Record Age, Control_Stage2). Once the row is inserted it will populate Control_OpenDate (01/27/2013) and Control_RecordAge (computed column, see formula).
(datediff(day, [Control_OpenDate], getdate()))
which gives the days up to date.
Everything is working perfect but I want to add like an IF condition to the computed column whenever the column Control_Stage2 is populated if not, do not calculate or add a text...
How can I add the WHERE-statement in the above formula??
Note: I'm entering such formula directly into the column properties, I know there are queries that can do this but is there a way to do it trough a formula.
This can be done using a CASE-statement, as shown here.
Your logic will then look like:
(CASE
WHEN [Control_Stage2] IS NULL THEN NULL -- or -1 or what you like
ELSE datediff(day,[Control_OpenDate],getdate())
END)
This can also be written as a ternary statement (IIF)
IIF ( boolean_expression, true_value, false_value )
IIF is a shorthand way for writing a CASE expression. It evaluates the Boolean expression passed as the first argument, and then returns either of the other two arguments based on the result of the evaluation. That is, the true_value is returned if the Boolean expression is true, and the false_value is returned if the Boolean expression is false or unknown
E.G.
iif([Control_Stage2] is null, null, datediff(day,[Control_OpenDate],getdate()))