简体   繁体   中英

SSRS: Display field using CAST in sql

I'm trying to display a value in the same field by using iif statement. But one is INT , another is Decimal and the last one is percentage. So, I CAST them as varchar in SQL. How do I display the result in SSRS field?

Here is an example:

IF @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

How do I display Result in a SSRS field?

Instead of embedding the logic in your SQL statement, why not do it in the report since (presumably) this is where @Choice is defined?

SQL:

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

Report:

Add a Calculated Field in the field listing of your DataSet (call it Result ):

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

Now your report will have 4 fields in the DataSet:

  • UserId (int)
  • IntNum (int)
  • DecNum (dec)
  • Result (string)

I think it would be best to do your logic in a CASE Statement, which is really one column, so it should work well in 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM