Jason Miesionczek
Jason Miesionczek

Reputation: 14448

Update table column with data from other columns in same row

I have a table that has several columns of textual data. The goal is to concatenate those columns into a single different column in the same table and same row.

What is the SQL Server query syntax that would allow me to do this?

Upvotes: 4

Views: 9542

Answers (4)

Pondlife
Pondlife

Reputation: 16240

Do you absolutely have to duplicate your data? If one of the column values changes, you will have to update the concatenated value.

A computed column:

alter table dbo.MyTable add ConcatenatedColumn = ColumnA + ColumnB

Or a view:

create view dbo.MyView as
select ColumnA, ColumnB, ColumnA + ColumnB as 'ConcatenatedColumn'
from dbo.MyTable

Now you can update ColumnA or ColumnB, and ConcatenatedColumn will always be in sync. If that's the behaviour you need, of course.

Upvotes: 3

Mark D
Mark D

Reputation: 5648

Might be misunderstanding but:

Alter table myTable add combinedColumn Varchar(1000);

Update myTable set combinedColumn = textField1 + textField2;

Upvotes: 1

heximal
heximal

Reputation: 10517

select 
  textfield1 + textfield2 + ... + textfieldN as conc_text,
  otherfield1,
  otherfield2,
  ...
  otherfieldN
from 
  mytable

Upvotes: 0

Paddy
Paddy

Reputation: 33857

Something like this:

UPDATE myTable SET X = Y + Z

Upvotes: 12

Related Questions