Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Updating one column according to another column

Author  Topic 

callinnn
Starting Member

7 Posts

Posted - 2007-05-22 : 01:15:32
I have my table like this

Code Amount Debit Credit
D 230
C 440
C 890
D 560

I want to update amount in Debit if Code is D
Else i want to update it into Credit

How can i establish this

Please Help

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-05-22 : 01:23:12
[code]Update Table
set Debit = case when Code = 'D' then Amount else 0 end,
set Credit = case when Code = 'C' then Amount else 0 end[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

shallu1_gupta
Constraint Violating Yak Guru

394 Posts

Posted - 2007-05-22 : 01:26:28
update Table Set Debit = case Code when 'D' then Amount end,
Credit = case Code when 'C' then Amount end
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-05-22 : 01:41:14
Use a Computed column? Bit of a waste re-storing the same value, and you runt he risk that the UPDATE doesn't run, or the values get out of sync etc.

Personally I would use a VIEW rather than a Computed column.

Kristen
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-05-22 : 01:46:58
quote:
Originally posted by harsh_athalye

Update Table
set Debit = case when Code = 'D' then Amount else 0 end,
set Credit = case when Code = 'C' then Amount else 0 end


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"




KH

Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-05-22 : 01:49:51
CnP syndrome!

Thanks KH.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-05-22 : 02:36:09
quote:
Originally posted by harsh_athalye

CnP syndrome!

Thanks KH.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"






KH

Go to Top of Page
   

- Advertisement -