【问题标题】:Using multiple VB Mid functions on a parameter in SSRS在 SSRS 中的参数上使用多个 VB Mid 函数
【发布时间】:2017-11-24 15:18:05
【问题描述】:

下面是我用来从用户 ID 中提取名字的表达式

"text\[forename] [Surname]"

目前,如果“ ”在名字和姓氏之间,则此表达是合适的。但是,我注意到的是“”可以替换为“。”或“/”或“_”例如。

我遇到的麻烦是包含多个“中间”功能以包含所有这些选项。我尝试使用“Or”、“Switch”和许多其他运算符,但没有任何效果。

我没有使用数据集,而是报告中的一个参数,即当用户登录报告时,他们的 UserID 被作为参数。

我是遗漏了一些明显的东西还是完全遗漏了标记?

任何帮助将不胜感激。

= "Hi " + Mid(Parameters!User.Value, InStr(Parameters!User.Value,"\")+1, (InStr(Parameters!User.Value, " ") - InStr(Parameters!User.Value, "\") - 1)) + "," &
chr(10) & chr(13) &
chr(10) & chr(13) &
"You have access to the Teams listed below. Only data from these teams will be shown in any analysis. Please click above to continue to the Dashboards" 

【问题讨论】:

  • 不要把这个放在一行...你可能想看看正则表达式。

标签: vb.net reporting-services


【解决方案1】:

Regex 绝对是解决此类问题的方法。这是一个如何使用正则表达式提取名字的示例:

Dim input As String = Parameters!User.Value
Dim pattern As String = "(?<=\\)[^ ./_]+"
Dim forename As String = Regex.Match(input, pattern).Value
Dim output As String = $"Hi {forename},

You have access to the Teams listed below. Only data from these teams will be shown in any analysis. Please click above to continue to the Dashboards"

VB.NET 现在允许多行字符串文字,因此无需使用Chr(10) &amp; Chr(13)。即使您不使用多行文字,根据您的需要使用Environment.NewLinevbCrLf 会更好。字符串文字前的$ 使它成为一个内插字符串,这意味着您可以在字符串中间的{} 括号中插入变量值。如果你不想使用它,你可以像以前一样连接:

Dim output As String = "Hi " & forename & 
    Environment.NewLine & 
    Environment.NewLine &
    "You have access to the Teams listed below. Only data from these teams will be shown in any analysis. Please click above to continue to the Dashboards"

正则表达式是搜索、解析和转换字符串的强大工具。如果您要对数据进行大量工作,那么学习它是非常值得的。

如果您不想使用正则表达式,我建议您使用String.Split,因为该方法允许您传递任意数量的分隔符。例如,您可以这样做:

Dim input As String = Parameters!User.Value
Dim parts() As String = input.Split("\"c, " "c, "."c, "/"c, "_"c)
Dim forename As String = If(parts.Length >= 2, parts(1), Nothing)

【讨论】:

  • 就使用 'IIF()' 语句而言,这也是一种可行的方法吗?还是那个方法效率太低?
  • 糟糕。感谢@NicoButler 让我重新注意到这一点。你让我重新阅读了原来的问题,这让我意识到我误读了这个问题。我认为可能是其他字符的斜线,但它是空格。我更新了正则表达式模式,使其能够正常工作。我还添加了一个示例,说明如何在没有正则表达式的情况下完成。 IIF(或者更好的是,只是 VB.NET 中较新的三元 If 运算符)肯定会起作用,但我担心它会变得有点长且不可读。未来增加更多灵活性也将更加困难。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多