dqm
dqm

Reputation: 1350

How can I merge 2 (or more) columns into a new one for each SQL row?

For example:

Field1 | Field2 | Field3 |
--------------------------
  the  |  lazy  |  dog

into

Field1 | Field2 | Field3 | History
--------------------------------------
  the  |  lazy  |  dog   |  thelazydog

Don't care about spaces etc.

Upvotes: 0

Views: 47

Answers (3)

Brian
Brian

Reputation: 13571

Use a virtual column. to handle summing null values use isNull.

CREATE TABLE Example
(
    ID int IDENTITY (1,1) NOT NULL
  , field1 smallint
  , field2 smallint
  , field3 smallint
  , history AS isnull(field1,0) + isnull(field2,0) + isNull(field3,0)
);

Upvotes: 2

John Smith
John Smith

Reputation: 7407

select *
, field1 + field2 + field 3 as history
into **put table name here**
from tblName

That's if you want it in a new table. If you just want to select the results, take out the 'into' line.

Upvotes: 0

Simon
Simon

Reputation: 1605

Try something like :

INSERT INTO Table (Field1, Field2, Field3, History) 
                  (Field1, Field2, Field3, Field1+Field2+Field3)

Upvotes: 0

Related Questions