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)
 Maths on three columns

Author  Topic 

Frosty615
Starting Member

13 Posts

Posted - 2011-04-19 : 09:15:35
Hi Guys,
I have a table that contains three columns which holds integers.
I need to do some swapping of the values
i.e.
Column 1 Column 2 Column 3
1 7 8
5 3 5

What I need to do is pass some of the values back to the previous column so that if Column1 isn't five or greater it needs to take the values from column 2 so make it five. Then it needs to look at column 2 and if that isn't five or more to look at column 3 and take some of the values from there.
So the output of the above would turn into

Column 1 Column 2 Column 3
5 5 6
5 5 3


Preferably I'd like this in a stored procedure. I don't want the solution but some pointers in the right direction would be great thanks

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-04-19 : 09:22:26
try this for col1/2

update tbl
set col1 = case when col1+col2 >= 5 then 5 else col1 + col2 end ,
col2 = case when col1+col2 <= 5 then 0 else col1+col2-5 end
where col1 < 5


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Frosty615
Starting Member

13 Posts

Posted - 2011-04-19 : 10:27:51
Thanks, that has worked a treat :)
Go to Top of Page
   

- Advertisement -