Reputation: 4862
I need to bring values together and copy it to a diffrent column.
COLUMN 1 | COLUMN 2 | COLUMN 3 | COLUMN 4
Hallo out there Hallo out there
My NULL name is My name is
I'm a rabbit I'm a rabbit
How to merge column 1, 2, 3 and copy it to column4 separated with space.
Columns can be null.
Upvotes: 0
Views: 1314
Reputation: 13506
update yourtable SET COlumn4= ltrim(rtrim(isnull([COLUMN 1],'') +' '+isnull([COLUMN 2],'') +' '+ isnull([COLUMN 3],'')))
Upvotes: 0
Reputation: 717
select isnull(convert(varchar(255),[COLUMN1]),'')+' '+isnull(convert(varchar(255),[COLUMN2]),'')+' '+isnull(convert(varchar(255),[COLUMN3]),'')
from table
UPDATE table
SET COLUMN4 = isnull(convert(varchar(255),[COLUMN1]),'')+' '+isnull(convert(varchar(255),[COLUMN2]),'')+' '+isnull(convert(varchar(255),[COLUMN3]),'')
Above codes may not work depending on datatypes so you may convert it to varchar before.
Upvotes: 0
Reputation: 453940
SQL Server 2012
UPDATE table
SET Column4 = CONCAT(Column1 + ' ', Column2 + ' ', Column3)
Upvotes: 3
Reputation: 51514
Using ISNULL
UPDATE table
SET Column4 =
ISNULL(Column1+' ','') +
ISNULL(Column2+' ','') +
ISNULL(Column3,'')
Or you could consider using a calculated column.
Upvotes: 1
Reputation: 280644
UPDATE dbo.table
SET column4 = COALESCE(column1, '')
+ COALESCE(' ' + column2, '')
+ COALESCE(' ' + column3, '');
Upvotes: 4
Reputation: 24116
Just use + sign
select ISNULL([COLUMN 1],'')+' ' +
isnull([COLUMN 2],'')+' ' +
isnull([COLUMN 3],'')
from your_table
Upvotes: 2