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
 update column with values from multiple columns

Author  Topic 

rob41
Yak Posting Veteran

67 Posts

Posted - 2010-10-22 : 10:42:36
How would i update a column with values from multiple columns.

I'm trying to update reason with values from load, ship, weight

load only has values of 1 or 0
ship only has values of 1 or 0
and weight only has values of 1 or 0

if load = 1
if ship = 1
if weight = 1

i want the reason column to have a value of load ship weight

maevr
Posting Yak Master

169 Posts

Posted - 2010-10-22 : 17:37:49
You could just convert the different columns to varchar and concaternate the strings. Just use query below in update.

select
cast(load as varchar(1)) +
cast(ship as varchar(1)) +
cast(weight as varchar(1)) as reason
from tbl
where
load = 1
and ship = 1
and weight = 1
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-10-23 : 01:47:54
so if all are 1 you want value of column as 111? what kind of design is that?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

rob41
Yak Posting Veteran

67 Posts

Posted - 2010-10-23 : 10:13:42
what i was trying to get is an update query that would update a row called codes with information from other columns, see below:

UPDATE [RC]
SET [RC].[CODE] = 'LMRC'
FROM [RC]
where LMRC = 1
UPDATE [RC]
SET [RC].[CODE] = 'XPD'
FROM [RC]
where XPD = 1
UPDATE [RC]
SET [RC].[CODE] = 'ACDT'
FROM [RC]


RESULT SHOULD BE

ID CODE
111 LMRC XPD ACDT
888 XPD
777 LMRC ACDT



1 IS JUST A FLAG THAT MEANS IT'S A VALID CODE
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-10-27 : 13:45:35
how did 888 and 777 came in between? your initial post values as 0 or 1

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

TimSman
Posting Yak Master

127 Posts

Posted - 2010-10-27 : 14:09:08
If I'm understanding this right, you want a column that shows which attribute flags are set. I may be wrong, but that's how I read it.
Go to Top of Page
   

- Advertisement -