Conditional column name

  • Good morning,

    I have a table of transactions.  If the transaction type is D for Debit I would like to return the transaction amount with a column name of Debit, else Credit.

    In psuedo-code

    DECLARE @type varchar(6)

    IF (transaction.type = 'D')

    SELECT @type = 'Debit'

    ELSE

    SELECT @type = "Credit"

    SELECT

    transactions.amount AS @type

    FROM

    transactions

    This approach is not working for me.  Any advice on how to do this?

    Thank you

    jmatt

     

  • select

    case when t.type = 'D' then t.amount

    end 'Debit'

    , case when t.type = 'C' then t.amount

    end 'credit'

    from Transactions t

    gets you them into two columns.

  • Thanks Steve!  That's exactly what I needed!

    Jonathan

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply