SSRS:在sql中使用CAST显示字段

时间:2015-03-19 20:56:04

标签: sql sql-server tsql reporting-services

我正在尝试使用iif语句在同一字段中显示值。但一个是INT,另一个是Decimal,最后一个是百分比。所以,我CAST在SQL中varcharIF @Choice = '1' BEGIN SELECT UserId, CAST(IntNum AS VarChar(10)) AS Result FROM Sample END ELSE IF @Choice = '2' BEGIN SELECT UserId, CAST(DecNum AS VarChar(10)) AS Result FROM Sample END 。如何在SSRS字段中显示结果?

以下是一个例子:

Result

如何在SSRS字段中显示{{1}}?

2 个答案:

答案 0 :(得分:1)

为什么不在报表中嵌入逻辑,为什么不在报表中这样做,因为(推测)这是定义@Choice的地方?

<强> SQL:

-- @Choice is not needed in this query.
SELECT UserId, IntNum, DecNum
FROM Sample

报告:

在DataSet的字段列表中添加计算字段(称之为Result):

=IIF(Parameters!Choice.Value = 1, Fields!IntNum.Value.ToString, Fields!DecNum.Value.ToString)

现在,您的报告将在DataSet中包含4个字段:

  • UserId(int)
  • IntNum(int)
  • DecNum(dec)
  • 结果(字符串)

答案 1 :(得分:0)

我认为最好在CASE语句中执行您的逻辑,这实际上是一列,所以它应该在SSRS中运行良好。

SELECT  UserID,
        CASE
            WHEN @Choice = 1 THEN CAST(IntNum AS VARCHAR(10))
            WHEN @Choice = 2 THEN CAST(DecNum AS VARCHAR(10))
            WHEN @Choice = 3 THEN CAST(PercentageNum AS VARCHAR(10))
        END AS Result
FROM    [Sample]