0

I am new to SQL. I have one banking project, in the in one table have values like this.

Type    Amount  
-----  -------

Credit   5000    
Debit    2000

Debit    1000........... so on

Now i need to Fetch these columns.Using Select Statement i can fetch, But i need to append '-' for amount which is Debited( like Debit '-2000'...) and '+' for Credited Amount.

How to append these functions in amount column Using Select Query. Kindly help me to solve this.

3 Answers 3

1

You can do this in a CASE statement checking the Type for Debit and then apply -1 to the amount:

SELECT
Type,
CASE WHEN Type='Debit' THEN (Amount*(-1)) ELSE Amount END AS Value
FROM Table
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your valuable reply
1

To show amount like -1000 and +2000 for Debit and Credit respecivelly, one should present those amount like 'strings'

select
    Type,
    case
        when Type = 'Credit' then '+' + convert(varchar, Amount)
        else '-' + convert(varchar, Amount)
    end as Amount
from
    tab

Comments

0

You can also use decode instead of case.

To print the signed value as string

select decode(type,'Debit','-','+')||amount as value from tab;

To keep the value as number but signed

select decode(type,'Debit',-1*amount,amount) as value from tab;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.