【问题标题】:SSRS expression split string with changing quantitiesSSRS 表达式拆分字符串,数量变化
【发布时间】:2016-01-30 19:59:47
【问题描述】:

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

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

并将其显示在我的 SSRS 报告拆分中,使其在报告单元格中显示为:

IMAGE01
IMAGE02
IMAGE03 

我面临的问题是我的 SQL 单元格中 IMAGExx 值的数量是完全随机的,SSRS 中有没有办法确定拆分字符串中有多少值,然后将正确的数量拆分成像上面的行?

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

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

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

编辑:

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

IMAGE01

IMAGE02

IMAGE03

【问题讨论】:

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


    【解决方案1】:

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

    转到Report manu,Report Properties / Code Tab 并将此代码放在文本区域中。

    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
    

    如果这对你有帮助,请告诉我。

    【讨论】:

    • 这看起来棒极了,现在就试试吧!
    • 这似乎给了我流氓额外的拆分新行。查看我的编辑
    • @PnP,您的字段中的单词似乎用双空格分隔。检查单词之间的空格
    • 啊,是的,你是对的,它还会在末尾生成一个新行,我们有什么办法可以删除尾随的新行?
    • 我的表中也有一些文件,名称类似于“IMAGE SYS01”。这现在也正在拆分,我可以使用 if 语句避免这种情况,例如 If it contains "SYS" do not split?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多