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 2008 Forums
 Transact-SQL (2008)
 Update column on all rows by multiplying cur value

Author  Topic 

jwigg5pt0
Starting Member

8 Posts

Posted - 2012-02-02 : 15:21:30
I am wanting to update a column in each row by multiplying the current value in the row by 4.

There are about 50,000+ rows in the table with unique incrementing id 'id'.
The column to be updated is 'Duration'. It is type int.
The data currently has either 1,2,3,4,5
Basically I want to iterate through each row, grab the current value, and update the row by the value * 4.

I've looked around a bit but the few variations I found don't seem to be working.

I can do this in .net but I'd rather see it go in SQL

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-02-02 : 15:41:38
[code]UPDATE yourTable SET Duration = 4*Duration;[/code]That would update EVERY row in yourTable by multiplying the value by 4. You don't need to iterate one row at a time. In fact, this type of set-based operation is what SQL excels at.
Go to Top of Page

jwigg5pt0
Starting Member

8 Posts

Posted - 2012-02-02 : 16:15:17
WOW I figured there was a VERY simple way to do it and not the SO complicated way I was coming across. Thank you
Go to Top of Page
   

- Advertisement -