I have an SSRS expression with this in it:
=IIF(
Fields!CUT.Value.ToString().Length > 1
,IIF(
Fields!CUT.Value.ToString().Contains(","),
Fields!CUT.Value.ToString().Substring(0, Fields!CUT.Value.ToString().IndexOf(",")),
Fields!CUT.Value.ToString()
)
,""
)
Translation:
If the field contains a ,, Get me the substring of the field from the first position to where the , is. If the field does not contain ,, just get me the entire field.
Probelm:
I get a #Error in all the pages where there is NO COMMA. If there is a COMMA, it works as expected. One of the fields, the value is 4. In a different scenario, the value is 17/64" Corrugated. I get an error for both of those.
What I've tried:
I've tried a few different approaches:
Used LIKE instead of Contains
=IIF(
IsNothing(Fields!CUT.Value)
,""
,IIF(
Fields!CUT.Value LIKE "*,*",
Mid(Fields!CUT.Value, InStr(Fields!CUT.Value, ",") + 1, (LEN(Fields!CUT.Value) - InStr(Fields!CUT.Value, ",") - 1))
Fields!CUT.Value
)
)
I added a new field to my dataset that replaces , with XXXX. I thought, maybe SSRS is not able to understand that I am talking about a , as a part of the string. And then changed the query to see if there is an XXXX in it. Still got the same #Error
Delete the .data file, cleaned the project, and rebuilt the project. Because, this is so obviously not incorrect. So I thought maybe it is not looking at the latest version of the report.
Nothing seems to do it! I am really close to doing something bad! This thing is driving me insane.
Questions:
Anyone see anything wrong with either of my expressions?
Is there a better way to do to what I am trying to accomplish? If there is a ,, give me everything before the ,. If not, give me everything.
Edit 1:
Worth noting that, in a different field, I am doing something very similar to this:
IIF(
IsNothing(Fields!CUT.Value)
,""
,IIF(
Fields!CUT.Value LIKE "*,*"
," | Length: " & Mid(Fields!CUT.Value, InStr(Fields!CUT.Value, ",") + 1, (LEN(Fields!CUT.Value) - InStr(Fields!CUT.Value, ",") - 1))
,""
)
)
What this is doing is, if there is a , in that very same field, get me everything AFTER the , and append it to | Length:. If there is no ,, just show an empty space "". This works perfectly fine. In situations where there is a , and where there is NO ,. So I've concluded that Microsoft is using SSRS to make me go crazy.
You could do this using SWITCH and test for no comma first but there is a much easier way assuming you only ever want to get the first part of the string up to the point of the comma.
This just uses the split function and takes the first element.
=(Split(Fields!CUT.Value, ",")).GetValue(0)
Nice and simple :)
Related
I need some help with an Access expression. I keep getting a syntax error.
I'm trying to get a calculated field from the combination of another calculated field.
Note that the LaserCuttingRate field is also a Lookup field. So it's not a simple entry field. It becomes a selector of the 3 choices.
IIf([LaserCuttingRate]="200",([CuttingTime]*200/60),IIf([LaserCuttingRate]="400",([CuttingTime]*400/60),IIf([LaserCuttingRate]="100",([CuttingTime]*100/60)))
All help appreciated.
Cam
I think you're missing a final close bracket.
There are 6 open brackets ( but only 5 closing brackets )
UPDATE - second issue
Two extra issues
The checks on number either don't need, or shouldn't have double-quotation masks
The final IIF also needs a false clause at the end (in the example below, 0).
Try this - I have put it into an Access database scratch table and it worked. (If relevant, note that I had LaserCuttingRate and CuttingTime set as doubles, as well as the Result Type for the calculated column).
IIf([LaserCuttingRate]=200,([CuttingTime]*200/60),IIf([LaserCuttingRate]=400,([CuttingTime]*400/60),IIf([LaserCuttingRate]=100,([CuttingTime]*100/60),0)))
I hope my title is clear enough. I'm working in MS Report Builder, using a function that applies a regular expression to a queried value in order to get back a certain substring. The regex works fine, so I'll demonstrate a simpler version here to make this less wordy. Here's the gist of my equation:
=IIF(Len(Fields!CourtLocation.Value) < 1, "none",System.Text.RegularExpressions.Regex.Match(Fields!CourtLocation.Value, "(?:[A-Z]{2,4})").Value))
The main purpose is to get that substring, but I added the IIF so that on those occasions when the CourtLocation.Value is empty (I tried Is Nothing in my expression as well), the function returns "none" rather than "#Error"
I've been looking around for a solution, but nothing has worked; it seems like most other people who talk about this are using a mathematical equation rather than trying to get a string. Can anyone help me get rid of that stupid "#Error" value?
You could try this (untested)
=System.Text.RegularExpressions.Regex.Match(
IIF(
Len(Fields!CourtLocation.Value) < 1,
"none",
Fields!CourtLocation.Value
)
, "(?:[A-Z]{2,4})"
).Value
This way the IIF is performed on the string that you want to pass to the regex function, so it always gets a valid value to process
Iif evaluates both sides, so you can nest two Iif statements to avoid the error.
Did you already read this one?
https://sqldusty.com/2011/08/01/ssrs-expression-iif-statement-divide-by-zero-error/
I'll copy the text into the answer if that solves it for you.
I have a matrix in ssrs 2008 and have an expression in one of its columns like:
= Fields!Name.Value & "(" & Fields!Score.Value & ")"
This is working fine and bringing the values inside the brackets like Jack(7.04) or John(6.39). Name is a nullable value, so if it is null then the value for this cell is simply like (7.23) or (6.11). My problem is that when I run the report and take the output as a csv files, the data who has NULL as their names are all converted to negative values without brackets in the output file like -7.23 or -6.11. I would like to see them like on the report output, (7.23) etc, on the csv file as well.
Current csv output
Jack(7.04)
John(6.39)
-7.23
-6.11
What I want
Jack(7.04)
John(6.39)
(7.23)
(6.11)
Note that it is working fine for pdf or excel outputs, it is only happening for csv outputs. How can I fix it? Any help would be appreciated.
Edit: It seems like it is happening because of '(' character. It is working when I use another, like '['. But I am supposed to use normal brackets '(' and ')'.
it's just a comment i don't have enough reputations to make comment.
Here are couple of thoughts.
1. Did you try with the text box properties
(Text box properties -> Number -> Negative numbers)
2. Try with if statement or switch statement=iif(Fields!score.Value>0,Fields!Name.Value & "(" & Fields!Score.Value & ")","(" & Fields!Score.Value & ")")
I'm trying to concatenate a string on the end of a sum, but if the sum is nothing, it breaks. It seems like this is due to SSRS evaluating both conditions of the IIf statement, but I can't figure out how to get around this.
I've got....
=IIf(IsNothing(Sum(Fields!Work.Value)), "", Sum(Fields!Work.Value).ToString + " J")
Which will print out the work summary + " J" if there is one, and #Error if not. What's the SSRS workaround?
Update / Clarification
The report in question is grouping on dates and then summing up Work, so it's not the case that Work is null, per se, but that for this particular date for this particular user, there are no rows in the group.. So, there are no rows to sum up in the error causing instance.
Sample Data Set
Name Date Work
Andy 12/1/15 511.30
Andy 12/1/15 549.70
Drew 12/2/15 484.80
Drew 12/2/15 322.36
Sample Report (current)
Name 12/1/15 12/2/15
Andy 1061 J #Error
Drew #Error 807.16 J
Sample Report (expected)
Name 12/1/15 12/2/15
Andy 1061 J
Drew 807.16 J
Have you considered doing the two parts of your desired output in two different expressions in the same Cell?
I assume for the current structure you have used a matrix, with Rows for the Name, and Columns for the date. You can the set the Data to be Sum of Work, as shown here, and in the red text in the image below.
=Sum(Fields!Work.Value)
You then then right click the cell and select "Create Placeholder" to insert a second expression in the same cell.
Set the value of this expression to be as shown here, and in the blue text below
=iif(Sum(Fields!Work.Value) > 0, " J", "")
Then when the report is run, it will always show the Sum if there is one, and if the value of the Sum is greater than zero, it will also display the J as required.
Hopefully this is the behaviour you require. Please let me know if you require further assistance with this solution
Try this:
=IIF(SUM(IIF(Isnothing(Fields!Total.Value),0,Fields!Total.Value)) = 0,
"",
SUM(IIF(Isnothing(Fields!Total.Value),0,Fields!Total.Value)).ToString + " J"
)
Let me know if this can help you
IIF does not short-circuit so this #Error is from SSRS trying to use the ToString function on NULLs.
The workaround is to add another IsNothing check in the false section before using ToString:
=IIF(IsNothing(Sum(Fields!Work.Value))
, ""
, IIF(IsNothing(Sum(Fields!Work.Value)), "", Sum(Fields!Work.Value)).ToString & " J")
To solve this exact problem, because if there is no Work to display then the report should display nothing, instead of using the IIf statement with concatenation, it is simple and sufficient to set a conditional visibility on the cell.
In the cell, use the expression:
Sum(Fields!Work.Value).ToString + " J"
Then for the same cell, select:
Text Box Properties > Visibility > Show or hide based on an expression
and enter:
=IsNothing(Sum(Fields!Work.Value))
While this solves this specific problem, if the solution requires the empty cell to display anything other than blank, then the original IIf short-circuit issue is still an issue.
I have a field like this in the column field in ssrs.
QUINIDINEssssssssssssssssssssssssssssssssssssssssssssssssssssss GL INJ 80MG/ML
FLECAINIDEssssssssssssssssssssssssssssssssss TAB 50MG
TAMBOCORsssssssssssssssssssssssssssssssssssssssssssss TAB 50MG
and how can i align align like this...
QUINIDINEssssssssssssssssssssssssssssssssssssssssssssssssssssss GL INJ 80MG/ML
FLECAINIDEssssssssssssssssssssssssssssssssss(emptyspaceeeeeeeee)TAB 50MG
TAMBOCORsssssssssssssssssssssssssssssssssssssssssssss(emptyspa)TAB 50MG
each part of string align properly...
Any help...
Well, there are two ways to do this that I worked out, and neither is particularly elegant so I'd be surprised if someone doesn't have a better solution.
"t.a" in the code below is your table and column.
The SQL way:
SUBSTRING(t.a,1,CHARINDEX(' ',t.a,1))+REPLICATE('_',20)+REVERSE(SUBSTRING(REVERSE(t.a),1,CHARINDEX(' ',REVERSE(t.a),1)))
Change the value being replicated above from '' to ' ' and you are in business. I left it as '' to illustrate what it's doing.
The SSRS way, which is better from a DB standpoint:
=Mid(Fields!a.Value,1,InStr(Fields!a.Value," "))+StrDup(20,"_")+StrReverse(Mid(StrReverse(Fields!a.Value),1,InStr(StrReverse(Fields!a.Value)," ")))
This is precisely the same formula, executed by SSRS instead of by SQL Server.
I once used Switch to append spaces to a column field, like in the following example:
=Switch(LEN(Fields!FamilyMemberName.Value) = 1, " ",
LEN(Fields!FamilyMemberName.Value) = 2, " ",
LEN(Fields!FamilyMemberName.Value) = 3, " ")
But I didn't have to bother about the expression getting longer, since I know exactly how many spaces I should append and it wasn't more than 15.
You could use Switch in your case too or you could write a VB function in the Code tab of Report Properties window and pass the values, use some loops to generate the spaces you require.
Or it's much easier in MS SQL, all you got to do is append SPACE(NoOfSpacesInInteger) to the field.