Reputation: 55
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......
Upvotes: 0
Views: 1586
Reputation: 368
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
Upvotes: 2