【问题标题】:difference between + and & in ssrs expressionssrs表达式中+和&的区别
【发布时间】:2015-12-15 09:51:44
【问题描述】:

我正在编写 SSRS 报告。

我有一个问题,ssrs-expression 中的+& 有什么区别?

请用简短的例子分享您的答案。谢谢。

【问题讨论】:

    标签: sql-server reporting-services ssrs-expression


    【解决方案1】:

    实际上这取决于您将如何使用它们。

    1.) 用于连接目的

    根据 MSDN:

    • 使用&+ 进行连接将产生相同的行为或输出。它只会连接两个字符串。

    例子:

    = 1 & 2
    

    输出: 12

    = "1" + "2"
    

    输出: 12

    2.) 用于算术目的

    根据 MSDN:

    • 使用+ 进行算术运算会将两个数字相加

    例子:

    = 1 + 2 //(using two numbers)
    

    输出: 3

    = 1 + "2" //(using a number and a number with quotes)
    

    输出: 3
    ----------------------------------------------- -------------------------
    例外情况

    现在有整数和字符串混淆或与其他类型混合的例外情况

    例子:

    = 1 + "two"
    

    输出: #Error -> 这是因为它们彼此不兼容。

    • 解决方法 - 使用转换函数将字段的默认数据类型转换为计算或组合文本所需的数据类型。

    例子:

    = CSTR(1) + "two"
    

    输出: 1two

    还有更多conversion functions,你可以根据需要使用。

    【讨论】:

      【解决方案2】:

      来自 Visual Studio 中的 SSRS 运算符描述:

      + 两个数字相加。也用于连接两个字符串。

      例子:

      =Fields!NumberCarsOwned.Value + 2
      /* if NumberCarsOwned=10, result: 10 + 2 = 12 */
      
      =Fields!String1.Value + Fields!String2.Value
      /* if String1='curious' and String2 ='guy', result: 'curiousguy' */
      
      ="4" + 5
      /* result: 9, implicit conversion of the first expression */
      
      ="a" + 5
      /* error: incorrect input string format */
      
      ="a" + CStr(5)
      /* result: a5 */
      
      ="a" + Str(5)
      /* result: a 5, space between 'a' and '5' */
      

      & 生成两个表达式的字符串连接。

      例子:

      =Fields!FirstName.Value & Fields!LastName.Value
      /* if FirstName='curious' and LastName='guy', result: 'curiousguy' */
      
      =4 & 5
      /* it's concatenation anyway, result: 45 */
      
      =CInt(4) & CInt(5)
      /* even explicit cast to integer it's concatenation anyway, result: 45 */
      

      另一个有用的运算符是And(逻辑/位与),它可能有助于解决一些任务。

      例子:

      =4 And 5
      /* result: 4, since 4 (100 binary) And 5 (101 binary) = 4 (100 binary) */
      ="4" And "5"
      /* result: 4 */
      ="a" And "b"
      /* error: incorrect input string format */
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-10
        • 1970-01-01
        • 1970-01-01
        • 2013-04-23
        • 2021-01-24
        • 2013-11-19
        • 2013-06-05
        相关资源
        最近更新 更多