0

I have a table called dbo.asientti. Inside, there is a column called "D/H". It contains identifiers: "D" for debit, and "H" for credit. On another column I have the debit and credit amount. The "D/H" column tells me if the amount is a credit or a debit.

What I want to do is separate them: that is, to create a column containing all the debit, and another column containing all the credit.

My guess is that I have to create some kind of conditional column. In other words, I want to tell the computer: "Create a column on dbo.asientti such that, if the value on "D/H" for amount x is "D", then x is inserted on the column". Then, I would do the same with "H" in order to have only the credit amounts.

That is, the debit amount on a new column D, and the credit amount H on another one.

Is there any way to do this on Transact SQL? I looked for information on conditional columns but I could not find anything.

Thanks in advance......

1
  • It's spelled amount - one "m" is quite enough! Commented Feb 16, 2018 at 17:26

1 Answer 1

2

What you are looking for are Computed Columns.

For your example it would work somehow like this

(*Updated for new requirements - amount is your amount column):

ALTER TABLE dbo.asientti
ADD 
D AS 
CASE WHEN DH = 'D' 
  THEN amount
  ELSE NULL
END,
H AS
CASE WHEN DH = 'C'
   THEN amount
   ELSE NULL
END
Sign up to request clarification or add additional context in comments.

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.