get a comma delimited string from rows [duplicate]
This question already has an answer here:
How to concatenate text from multiple rows into a single text string in SQL server?
44 answers
What will be the query in MS Sql Server to concatenate my rows in one string , delimited by comma (Like shown below)
sql sql-server sql-server-2012
marked as duplicate by krokodilko, Shnugo
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 22 '17 at 10:37
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How to concatenate text from multiple rows into a single text string in SQL server?
44 answers
What will be the query in MS Sql Server to concatenate my rows in one string , delimited by comma (Like shown below)
sql sql-server sql-server-2012
marked as duplicate by krokodilko, Shnugo
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 22 '17 at 10:37
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Which DBMS are you using? Oracle or SQL Server? And Have you tried something?
– JSapkota
Jan 22 '17 at 6:06
SQL SERVER, i have no idea how to do.....
– Ajit Kumar
Jan 22 '17 at 6:33
1
Please have a look here stackoverflow.com/q/194852/1530987
– bhageena
Jan 22 '17 at 6:46
Please read How to Ask.
– Zohar Peled
Jan 22 '17 at 8:07
add a comment |
This question already has an answer here:
How to concatenate text from multiple rows into a single text string in SQL server?
44 answers
What will be the query in MS Sql Server to concatenate my rows in one string , delimited by comma (Like shown below)
sql sql-server sql-server-2012
This question already has an answer here:
How to concatenate text from multiple rows into a single text string in SQL server?
44 answers
What will be the query in MS Sql Server to concatenate my rows in one string , delimited by comma (Like shown below)
This question already has an answer here:
How to concatenate text from multiple rows into a single text string in SQL server?
44 answers
sql sql-server sql-server-2012
sql sql-server sql-server-2012
edited Jan 22 '17 at 8:58
Hadi
16.4k62570
16.4k62570
asked Jan 22 '17 at 5:58
Ajit Kumar
1513
1513
marked as duplicate by krokodilko, Shnugo
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 22 '17 at 10:37
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by krokodilko, Shnugo
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 22 '17 at 10:37
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Which DBMS are you using? Oracle or SQL Server? And Have you tried something?
– JSapkota
Jan 22 '17 at 6:06
SQL SERVER, i have no idea how to do.....
– Ajit Kumar
Jan 22 '17 at 6:33
1
Please have a look here stackoverflow.com/q/194852/1530987
– bhageena
Jan 22 '17 at 6:46
Please read How to Ask.
– Zohar Peled
Jan 22 '17 at 8:07
add a comment |
Which DBMS are you using? Oracle or SQL Server? And Have you tried something?
– JSapkota
Jan 22 '17 at 6:06
SQL SERVER, i have no idea how to do.....
– Ajit Kumar
Jan 22 '17 at 6:33
1
Please have a look here stackoverflow.com/q/194852/1530987
– bhageena
Jan 22 '17 at 6:46
Please read How to Ask.
– Zohar Peled
Jan 22 '17 at 8:07
Which DBMS are you using? Oracle or SQL Server? And Have you tried something?
– JSapkota
Jan 22 '17 at 6:06
Which DBMS are you using? Oracle or SQL Server? And Have you tried something?
– JSapkota
Jan 22 '17 at 6:06
SQL SERVER, i have no idea how to do.....
– Ajit Kumar
Jan 22 '17 at 6:33
SQL SERVER, i have no idea how to do.....
– Ajit Kumar
Jan 22 '17 at 6:33
1
1
Please have a look here stackoverflow.com/q/194852/1530987
– bhageena
Jan 22 '17 at 6:46
Please have a look here stackoverflow.com/q/194852/1530987
– bhageena
Jan 22 '17 at 6:46
Please read How to Ask.
– Zohar Peled
Jan 22 '17 at 8:07
Please read How to Ask.
– Zohar Peled
Jan 22 '17 at 8:07
add a comment |
2 Answers
2
active
oldest
votes
Use STUFF
and FOR XML
:
Create and populate sample table (Please save us this step in your future questions)
DECLARE @T AS TABLE
(
Name varchar(10)
)
INSERT INTO @T VALUES
('John'),
('Vicky'),
('Sham'),
('Anjli'),
('Manish')
The query:
SELECT STUFF((
SELECT ',' + Name
FROM @T
FOR XML PATH('')
), 1, 1, '') As [output];
Results:
output
John,Vicky,Sham,Anjli,Manish
add a comment |
Assuming That your column name is NAME
And table name is MYTABLE
you can use the Following query:
DECLARE @strTemp VARCHAR(MAX)
SET @strTemp = ''
SELECT @strTemp = @strTemp + ISNULL(NAME,'') + ','
FROM MYTABLE
--Remove last comma
SET @strTemp = SUBSTRING(@strTemp ,1,LEN(@strTemp ) -1)
--Get Result
SELECT @strTemp
You can filter null records using the following
SELECT @strTemp = @strTemp + NAME + ','
FROM MYTABLE
WHERE NAME IS NOT NULL
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use STUFF
and FOR XML
:
Create and populate sample table (Please save us this step in your future questions)
DECLARE @T AS TABLE
(
Name varchar(10)
)
INSERT INTO @T VALUES
('John'),
('Vicky'),
('Sham'),
('Anjli'),
('Manish')
The query:
SELECT STUFF((
SELECT ',' + Name
FROM @T
FOR XML PATH('')
), 1, 1, '') As [output];
Results:
output
John,Vicky,Sham,Anjli,Manish
add a comment |
Use STUFF
and FOR XML
:
Create and populate sample table (Please save us this step in your future questions)
DECLARE @T AS TABLE
(
Name varchar(10)
)
INSERT INTO @T VALUES
('John'),
('Vicky'),
('Sham'),
('Anjli'),
('Manish')
The query:
SELECT STUFF((
SELECT ',' + Name
FROM @T
FOR XML PATH('')
), 1, 1, '') As [output];
Results:
output
John,Vicky,Sham,Anjli,Manish
add a comment |
Use STUFF
and FOR XML
:
Create and populate sample table (Please save us this step in your future questions)
DECLARE @T AS TABLE
(
Name varchar(10)
)
INSERT INTO @T VALUES
('John'),
('Vicky'),
('Sham'),
('Anjli'),
('Manish')
The query:
SELECT STUFF((
SELECT ',' + Name
FROM @T
FOR XML PATH('')
), 1, 1, '') As [output];
Results:
output
John,Vicky,Sham,Anjli,Manish
Use STUFF
and FOR XML
:
Create and populate sample table (Please save us this step in your future questions)
DECLARE @T AS TABLE
(
Name varchar(10)
)
INSERT INTO @T VALUES
('John'),
('Vicky'),
('Sham'),
('Anjli'),
('Manish')
The query:
SELECT STUFF((
SELECT ',' + Name
FROM @T
FOR XML PATH('')
), 1, 1, '') As [output];
Results:
output
John,Vicky,Sham,Anjli,Manish
edited Jan 22 '17 at 8:03
answered Jan 22 '17 at 7:55
Zohar Peled
52.3k73273
52.3k73273
add a comment |
add a comment |
Assuming That your column name is NAME
And table name is MYTABLE
you can use the Following query:
DECLARE @strTemp VARCHAR(MAX)
SET @strTemp = ''
SELECT @strTemp = @strTemp + ISNULL(NAME,'') + ','
FROM MYTABLE
--Remove last comma
SET @strTemp = SUBSTRING(@strTemp ,1,LEN(@strTemp ) -1)
--Get Result
SELECT @strTemp
You can filter null records using the following
SELECT @strTemp = @strTemp + NAME + ','
FROM MYTABLE
WHERE NAME IS NOT NULL
add a comment |
Assuming That your column name is NAME
And table name is MYTABLE
you can use the Following query:
DECLARE @strTemp VARCHAR(MAX)
SET @strTemp = ''
SELECT @strTemp = @strTemp + ISNULL(NAME,'') + ','
FROM MYTABLE
--Remove last comma
SET @strTemp = SUBSTRING(@strTemp ,1,LEN(@strTemp ) -1)
--Get Result
SELECT @strTemp
You can filter null records using the following
SELECT @strTemp = @strTemp + NAME + ','
FROM MYTABLE
WHERE NAME IS NOT NULL
add a comment |
Assuming That your column name is NAME
And table name is MYTABLE
you can use the Following query:
DECLARE @strTemp VARCHAR(MAX)
SET @strTemp = ''
SELECT @strTemp = @strTemp + ISNULL(NAME,'') + ','
FROM MYTABLE
--Remove last comma
SET @strTemp = SUBSTRING(@strTemp ,1,LEN(@strTemp ) -1)
--Get Result
SELECT @strTemp
You can filter null records using the following
SELECT @strTemp = @strTemp + NAME + ','
FROM MYTABLE
WHERE NAME IS NOT NULL
Assuming That your column name is NAME
And table name is MYTABLE
you can use the Following query:
DECLARE @strTemp VARCHAR(MAX)
SET @strTemp = ''
SELECT @strTemp = @strTemp + ISNULL(NAME,'') + ','
FROM MYTABLE
--Remove last comma
SET @strTemp = SUBSTRING(@strTemp ,1,LEN(@strTemp ) -1)
--Get Result
SELECT @strTemp
You can filter null records using the following
SELECT @strTemp = @strTemp + NAME + ','
FROM MYTABLE
WHERE NAME IS NOT NULL
edited Jan 22 '17 at 9:07
answered Jan 22 '17 at 8:54
Hadi
16.4k62570
16.4k62570
add a comment |
add a comment |
Which DBMS are you using? Oracle or SQL Server? And Have you tried something?
– JSapkota
Jan 22 '17 at 6:06
SQL SERVER, i have no idea how to do.....
– Ajit Kumar
Jan 22 '17 at 6:33
1
Please have a look here stackoverflow.com/q/194852/1530987
– bhageena
Jan 22 '17 at 6:46
Please read How to Ask.
– Zohar Peled
Jan 22 '17 at 8:07