We help IT Professionals succeed at work.

SQL - Combine multiple fields from a table into a single column

ttist25
ttist25 asked
on
97 Views
Last Modified: 2018-09-26
Hello,

I'm trying to concatenate the values in multiple columns into a string in another column.

Here is some SQL:
CREATE TABLE #tmpExample ( 
    column1 varchar(255),
    column2 varchar(255),
    column3 varchar(255),
	column4 varchar(255)
);
INSERT INTO #tmpExample  
VALUES 
('1', 'A', 'B', 'C'),
('2', 'D', 'E', 'F'),
('3', 'G', 'H', 'I'),
('4', '', '4', 'ME');

ALTER TABLE #tmpExample
ADD columns2_4 varchar(255)

Open in new window


Given that data I would like to write a statement that results in the following in "columns2_4":
ABC
DEF
GHI
PIZZA4ME

I'm hungry right now and so I'm thinking ISNULL(COLUMN2, 'PIZZA').

Actually any NULL in column 2,3, or 4 should return pizza (I'm REALLY hungry).  

TIA!
Comment
Watch Question

Senior DBA
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
Great thanks Brendt!