August 22, 2018 at 1:21 pm
I want to combine rows into a single row
below is the data
create table test
(
col1 varchar(5) null,
col2 varchar(5) null
)
go
insert into test values ('A', 'B')
insert into test values ('C','D')
insert into test values ('E','F')
select
* from test
yields:
col1col2
A B
C D
E F
I want one
row
with values A B C D E F
how can this be done?
August 22, 2018 at 1:35 pm
jdswarrior - Wednesday, August 22, 2018 1:21 PMI want to combine rows into a single row
below is the datacreate table test
(
col1 varchar(5) null,
col2 varchar(5) null
)
go
insert into test values ('A', 'B')
insert into test values ('C','D')
insert into test values ('E','F')select * from test
yields:col1col2
A B
C D
E F
I want one row with values A B C D E F
how can this be done?
Try this:DROP TABLE IF EXISTS #test;
CREATE TABLE #test
(
col1 VARCHAR(5) NULL
, col2 VARCHAR(5) NULL
);
GO
INSERT #test
VALUES
(
'A', 'B'
)
,(
'C', 'D'
)
,(
'E', 'F'
);
DECLARE @X VARCHAR(MAX) = '';
SELECT @X = CONCAT(@X, t.col1, ' ', t.col2, ' ')
FROM #test t;
SELECT @X;
August 22, 2018 at 1:36 pm
Have you tried anything so far? If not, consider the simple + operator and FOR XML PATH. Have a go, and if you get stuck please do reply with your attempt so that we can help you further.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
August 22, 2018 at 2:16 pm
Phil Parkin - Wednesday, August 22, 2018 1:35 PMjdswarrior - Wednesday, August 22, 2018 1:21 PMI want to combine rows into a single row
below is the datacreate table test
(
col1 varchar(5) null,
col2 varchar(5) null
)
go
insert into test values ('A', 'B')
insert into test values ('C','D')
insert into test values ('E','F')select * from test
yields:col1col2
A B
C D
E F
I want one row with values A B C D E F
how can this be done?
Try this:
DROP TABLE IF EXISTS #test;
CREATE TABLE #test
(
col1 VARCHAR(5) NULL
, col2 VARCHAR(5) NULL
);
GOINSERT #test
VALUES
(
'A', 'B'
)
,(
'C', 'D'
)
,(
'E', 'F'
);DECLARE @X VARCHAR(MAX) = '';
SELECT @X = CONCAT(@X, t.col1, ' ', t.col2, ' ')
FROM #test t;SELECT @X;
yes that works - I should have been more specific - can I get output values in 6 separate columns?
August 22, 2018 at 2:55 pm
jdswarrior - Wednesday, August 22, 2018 2:16 PMPhil Parkin - Wednesday, August 22, 2018 1:35 PMjdswarrior - Wednesday, August 22, 2018 1:21 PMI want to combine rows into a single row
below is the datacreate table test
(
col1 varchar(5) null,
col2 varchar(5) null
)
go
insert into test values ('A', 'B')
insert into test values ('C','D')
insert into test values ('E','F')select * from test
yields:col1col2
A B
C D
E F
I want one row with values A B C D E F
how can this be done?
Try this:
DROP TABLE IF EXISTS #test;
CREATE TABLE #test
(
col1 VARCHAR(5) NULL
, col2 VARCHAR(5) NULL
);
GOINSERT #test
VALUES
(
'A', 'B'
)
,(
'C', 'D'
)
,(
'E', 'F'
);DECLARE @X VARCHAR(MAX) = '';
SELECT @X = CONCAT(@X, t.col1, ' ', t.col2, ' ')
FROM #test t;SELECT @X;
yes that works - I should have been more specific - can I get output values in 6 separate columns?
I have to ask WHY? It doesn't scale if you don't use dynamic SQL, and it won't scale well if you do. If this is for a report, do this in your reporting software.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
August 22, 2018 at 3:54 pm
jdswarrior - Wednesday, August 22, 2018 2:16 PMPhil Parkin - Wednesday, August 22, 2018 1:35 PMTry this:DROP TABLE IF EXISTS #test;
CREATE TABLE #test
(
col1 VARCHAR(5) NULL
, col2 VARCHAR(5) NULL
);
GOINSERT #test
VALUES
(
'A', 'B'
)
,(
'C', 'D'
)
,(
'E', 'F'
);DECLARE @X VARCHAR(MAX) = '';
SELECT @X = CONCAT(@X, t.col1, ' ', t.col2, ' ')
FROM #test t;SELECT @X;
yes that works - I should have been more specific - can I get output values in 6 separate columns?
I'm not saying this is a good solution, but this adjustment to Phil's solution will make them into columns...
DROP TABLE IF EXISTS #test;
CREATE TABLE #test
(
col1 VARCHAR(5) NULL
, col2 VARCHAR(5) NULL
);
GO
INSERT #test
VALUES
('A', 'B'),('C', 'D'),('E', 'F');
DECLARE @X VARCHAR(MAX) = '';
SELECT @X =
'select ' + STUFF(
(SELECT DISTINCT ', ''' + t.[col1] + ''' , ''' + t.[col2] + '''' , ' '
FROM #test t FOR XML PATH(''), TYPE
).value('.', 'nvarchar(max)'), 1, 1, '');
EXEC (@x);
August 24, 2018 at 12:22 pm
work well - thx - i have an alternative solution already developed - wanted to compare the two and see what works best in powershell - we are looping thru a list of over 200 servers in a text file and want to run a SQLCMD and get a list of linked servers and data_source names from sys.servers
Viewing 7 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply