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.
| Author |
Topic |
|
satish.gorijala
Posting Yak Master
182 Posts |
Posted - 2009-01-09 : 07:45:04
|
| Hi i have a table with three columnscol1 col2 Result1 4 null2 2 null8 3 null6 5 nullNow i want to add col1 & col2 and insert in to column "result"the ouput should be like thiscol1 col2 Result1 4 52 2 48 3 116 5 11How to write query for this in sqlserver? G. Satish |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-09 : 07:50:47
|
| update yourtableset result = isnull(col1,0)+isnull(col2,0)select col1,col2,result from yourtable |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-01-09 : 07:51:51
|
| Dont need a third columnselect col1,col2,col1+col2 as result from your_tableMadhivananFailing to plan is Planning to fail |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-01-09 : 07:53:02
|
| update tset result = s.sumfrom tab tinner join(select col1,col1 + col2 as sum from tab)s on s.col1 = t.col1 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-01-09 : 07:54:44
|
quote: Originally posted by bklr update tset result = s.sumfrom tab tinner join(select col1,col1 + col2 as sum from tab)s on s.col1 = t.col1
Your first reply is the correct oneMadhivananFailing to plan is Planning to fail |
 |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2009-01-09 : 07:55:42
|
| U can do this by using computed columnsJai Krishna |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-09 : 08:31:41
|
| no need of seperate column at all as Madhi suggests. just calculate on the fly in select statement |
 |
|
|
|
|
|