SQL Reporting 2008; Check if an Array Contains a String - reporting-services

In SQL Reporting 2008 how can I determine if an Array Contains a String?
Example, I wish the following to return "1":
IIf(Split("a,b,c", ",").CONTAINS("a"), "1", "0")
What may be used in replace of the above CONTAINS function? Is it impossible? This value'd be the FilterExpression for my table. Its purpose is to decide what to show and what to hide.

If you are looking for an answer only in an expression, I am not positive. However, you can write .Net methods and call them just like expressions from a custom dll or a "code" section of the report. If you use built-in code, you can do something like the following:
http://www.vbforums.com/showthread.php?t=558440
Creating inline code or referencing an assembly in SSRS:
http://bryantlikes.com/pages/824.aspx
UPDATE:
Example to get your delimited values from your concatenated string:
http://www.dotnetperls.com/split-vbnet
UPDATE:
Here is a function you can use. You put it in the code section of the report:
Public Function Contains(ByVal ItemToCheck As String, ByVal CommaValuesList As String, ByVal delimeter As Char) As Boolean
Dim commaValues() As String = Split(CommaValuesList, delimeter, -1, CompareMethod.Text)
For Each commavalue As String In commaValues
If ItemToCheck.ToLower.Trim = commavalue.ToLower.Trim Then
Return True
End If
Next
Return False
End Function
Use the following syntax to reference it:
=code.Contains(param1,param2,param3)

Let's use MyLettersParameter as a multiselect parameter. To determine if it contains "a" use:
=Array.IndexOf(Parameters!MyLettersParameter.Value, "a") > -1
The above code returns true or false. To return "1" use:
=IIf(Array.IndexOf(Parameters!MyLettersParameter.Value, "a") > -1, "1", "0")

Related

Index was outside the bounds of the array in SSRS

I have two parameters , let's say P1 and P2. The sample expression I used for P2 is
IIF(P1.Label="string", "null" ,Split(P1.Label," ").GetValue(0))
When the condition is false, the split expression is working fine. But if the condition is true, I'm getting 'Index was outside the bounds of the array' error. If the condition is true, I need to pass the value "null" as varchar type.
Can someone please advice on this?
The problem with the IIF function is that it is a function not a language construct. This means that it evaluates both parameters before passing the parameters to the function. Consequently, if you are trying to do a Split on a parameter that can't be split, you will still get the 'Index was outside the bounds of the array' error, even when it looks like that code shouldn't be executed due to boolean condition of the IIF statement.
The best way to solve this is to create a safe string splitter function in custom code where you can use real language constructs. Also, check that the string is splittable by checking it contains a space instead of checking for a special string:
Public Function SafeSplit(ByVal SplitThis As String) As String
If InStr(SplitThis, " ") Then
Return Split(SplitThis, " ")(0)
End If
Return "null"
End Function
and then use this in your report for the Value expression instead of IIF:
=Code.SafeSplit(Parameters!P1.Label)

Use Split, Join, and another function in SSRS

I have a field in SQL Server that contains an comma separated list. Here are 2 examples:
select 'ex1,ex2,ex3' as str union all
select 'ax1,ax2'
In my report, I have to transform all of these values (5 in this case) using a function. In this question I will use Trim, but in actuality we are using another custom made function with the same scope.
I know how I can split every value from the string and recombine them:
=Join(Split(Fields!str.Value,","),", ")
This works great. However, I need to execute a function before I recombine the values. I thought that this would work:
=Join( Trim(Split(Fields!VRN.Value,",")) ,", ")
However, this just gives me an error:
Value of type '1-dimensional array of String' cannot be converted to 'String'. (rsCompilerErrorInExpression)
I can't personally change the function that we use.
How do I use an extra function when dealing with both an split and a join?
You can use custom code to include all the logic (Split->Custom Code->Join).
Make adjustments inside the loop to call your custom function instead of trim
Public Function fixString (ByVal s As String) As String
Dim mystring() As String
mystring = s.Split(",")
For index As Integer = 0 To mystring.Length-1
mystring(index) = Trim(mystring(index))
Next
Return Join(mystring, ",")
End Function
To call the custom code use the following expression
Code.fixString( Fields!VRN.Value )

NOT IN in SSRS TextBox

How can I write NOT IN in TextBox expression?
I must check if some field value not belong to some list of strings, and then do some work.
Example:
Iif(SomeField.Value NOT IN ('Text1', 'Text2'), DoSomething, nothing)
I wrote code like this and got error when previewing report, and error was :
Overload resolution failed because no accessible 'Iif' accepts this number of type arguments
How can I do this stuff?
Try this small piece of custom code that accepts a string array. Just paste it into the report code section of the report..
Public Shared Function ValueExists(ByVal arr() as string, checkVal as string)
Dim i As Long
For i = LBound(arr) To UBound(arr)
If arr(i) = checkVal Then
return true
Exit Function
End If
Next i
return false
End Function
Usage would involve splitting the string into an array using the Split function
like so:
=iif(Code.ValueExists(Split("Your,comma,separated,string,in,here",","),"StringYouWantToFind")
,"Your value exists"
,"your value does not exist")
You can simply write the code like this:
Iif(SomeField.Value <> 'Text1' AND Field.Value <> 'Text2' , DoSomething, nothing)
I got this one in one report:
=iif(join(Parameters!Parameter1.Value,",") like "*" & Fields!Field1.Value & "*","Color1","Color2")
This instruction helps me to determine the fill colour of a cell inside a tablix, where:
Parameter1 is a multivalue parameter.
"Join" lets me have a string with all selected values from a multivalue parameter, eg. "value1,value2,value3,value4"
Field1 is the field that contains the values filtered by Parameter1
Color1 is the color if the value of the cell is included in the selection of parameter
else Color2
works well

SSIS Convert Blank or other values to Zeros

After applying the unpivot procedure, I have an Amount column that has blanks and other characters ( like "-"). I would like to convert those non-numberic values to zero. I use replace procedure but it only converts one at the time.
Also, I tried to use the following script
/**
Public Overrides Sub Input()_ProcessInputRows(ByVal Row As Input()Buffer)
If Row.ColumnName_IsNull = False Or Row.ColumnName = "" Then
Dim pattern As String = String.Empty
Dim r As Regex = Nothing
pattern = "[^0-9]"
r = New Regex(pattern, RegexOptions.Compiled)
Row.ColumnName = Regex.Replace(Row.ColumnName, pattern, "")
End If
End Sub
**/
but i'm getting error.I don't much about script so maybe I placed in the wrong place. The bottom line is that I need to convert those non-numberic values.
Thank you in advance for your help.
I generally look at regular expressions as a great way to introduce another problem into an existing one.
What I did to simulate your problem was to write a select statement that added 5 rows. 2 with valid numbers, the rest were an empty string, string with spaces and one with a hyphen.
I then wired it up to a Script Component and set the column as read/write
The script I used is as follows. I verified there was a value there and if so, I attempted to convert the value to an integer. If that failed, then I assigned it zero. VB is not my strong suit so if this could have been done more elegantly, please edit my script.
Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
' Ensure we have data to work with
If Not Row.ColumnName_IsNull Then
' Test whether it's a number or not
' TryCast doesn't work with value types so I'm going the lazy route
Try
' Cast to an integer and then back to string because
' my vb is weak
Row.ColumnName = CStr(CType(Row.ColumnName, Integer))
Catch ex As Exception
Row.ColumnName = 0
End Try
End If
End Sub

Populating multiple values in rdlc reporting

I am using rdlc report, i have a column in database which i want to display in the report.
vehicleDamageArea=1,2,3
In the report I need to mark the placeholders with these values.
=iif((Fields!vehicleDamageArea.Value="3"),Chr(253),Chr(168)) like this.
But as we know,it will check the whole value 1,2,3="3" not the splitted values.
Any suggestion to check by splitting the vehicleDamageArea parameter.
I made it to work as below
Public Shared Function CheckValue(ByVal InString As String,ByVal input as String) As Char
Dim output As String = String.Empty
Dim Parts() As String = InString.ToString().Split(",")
For i As Integer = 0 To Parts.Length - 1
If Parts(i) = input Then
output = Chr(0120)
Exit For
Else
output = Chr(0111)
End If
Next i
Return output
End Function
You can get the individual values using the split function in reporting services. It returns a zero-based string array, so for your example you need
=Split(First(Fields!ID.Value),",")(2)
You should make a function that accept a comma separated expression, than process this string and return a Boolean, then call this function as for boolean value.