SSRS表达式拆分数量变化的字符串

时间:2016-01-30 19:59:48

标签: sql reporting-services reporting ssrs-2008-r2

我已使用以下文章(ssrs expression to split string possible?)允许我以这种格式从SQL列中获取数据:

onActivityResult()

并将其显示在我的SSRS报告中,以便它在报告单元格中显示为:

IMAGE01 IMAGE02 IMAGE03 (all these values are in one field)

我面临的问题是我的SQL单元格中IMAGE01 IMAGE02 IMAGE03 值的数量是完全随机的,SSRS中是否有一种方法可以确定拆分字符串中有多少值然后拆分正确的数量像上面这样的行?

现在我的表达是这样的(用于测试):

IMAGExx

但我不知道给定字段中是否有2个值或9个值。

修改

下面的代码非常棒,但在值之间给了我一个额外的新行:

=(Split(Fields!something.Value, " ")).GetValue(0) & vbcrlf & (Split(Fields!something.Value, " ")).GetValue(2)

1 个答案:

答案 0 :(得分:1)

我认为您可以使用自定义代码来解决您的问题。

转到Report manu,Report Properties / Code标签,并将此代码放入文字区域。

Public Function mySplit(ByVal my_field As String) As String
    Dim result As String = ""       
    If my_field is Nothing or my_field = "" Then
        Return result
    End If
    Dim TestArray() As String = my_field.Split(New Char() {" "c})
    Dim word as String      
    For Each word In TestArray
        result = result + word + vbCrLf 
    Next
    Return result

End Function

将功能添加到报告后,您可以从表达式

中调用它
=Code.mySplit(Fields!myField.Value)

它会根据单个空格("")拆分值,并返回由新行vbCrLf分隔的值。

示例:

=Code.mySplit("This is a test")

它返回:

This
is
a
test

更新:功能不会返回额外的新行。

Public Function mySplit(ByVal my_field As String) As String
    Dim result As String = ""       
    If my_field is Nothing or my_field = "" Then
        Return result
    End If
    Dim TestArray() As String = my_field.Split(New Char() {" "c})
    Dim word as String
    Dim counter as Integer = 0      
    For Each word In TestArray
        counter = counter +1
        If counter = TestArray.Length Then
            result = result + word
        Else
                result = result + word + vbCrLf 
        End if
    Next
    Return result

End Function

如果这有助于您,请告诉我。