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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 conditional updateing of a column

Author  Topic 

Ali T
Starting Member

22 Posts

Posted - 2009-02-01 : 09:23:25
dear all
I have a table with two columns A and B. I want to insert the content of column A Which are numbers, to column B under the following condition:
If the the record in A is positive insert the same quantity into column B but if it is negative (0>) multiply it by (-1) and then insert it into Column B.
and Now aI need the query by using UPDATE command.
help me with this please.

Ragards
Ali

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-01 : 10:40:56
[code]Update T
Set T.B =(Case when T.A >0 then T.A else T.A *(-1) end)
from Table T
[/code]
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-02-01 : 11:31:44
So you want always positive values in column B?

Update Table
set B = ABS(A)


Greetings
Webfred


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-01 : 12:30:08
[code]
Update T
Set T.B =SIGN(T.A)* T.A
from Table T
[/code]
Go to Top of Page
   

- Advertisement -