September 18, 2018 at 11:14 am
Please run my code and you will see what I mean.
So I like to concatenate values in a column and get one row of output.
The solution below works.. But the column name it gives has a wierd guid.
How ca we chane that column name
if object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;
Create table #t(mm VARCHAR(100) )
INSERT INTO #t( mm) VALUES ( 'ABC' );
INSERT INTO #t( mm) VALUES ( 'DDD' );
INSERT INTO #t( mm) VALUES ( 'XXX' );
INSERT INTO #t( mm) VALUES ( 'YYY' );
Select * FROM #t;
--Instead of 4 rows I would like one line and the
--output should look like
--ABC,DDD,XXX,YYY
-- I Know there is a some nice way to do this using XML SYNTAX
-- and without using the CONCATENATE FUNCTION
--The following work but gives a wierd column name.
--How can we rename the column name to something readable ?
SELECT ',' + mm AS [text()]
FROM #t
FOR XML PATH('')
September 18, 2018 at 11:22 am
mw_sql_developer - Tuesday, September 18, 2018 11:14 AMPlease run my code and you will see what I mean.
So I like to concatenate values in a column and get one row of output.
The solution below works.. But the column name it gives has a wierd guid.
How ca we chane that column name
if object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;Create table #t(mm VARCHAR(100) )
INSERT INTO #t( mm) VALUES ( 'ABC' );
INSERT INTO #t( mm) VALUES ( 'DDD' );
INSERT INTO #t( mm) VALUES ( 'XXX' );
INSERT INTO #t( mm) VALUES ( 'YYY' );Select * FROM #t;
--Instead of 4 rows I would like one line and the
--output should look like
--ABC,DDD,XXX,YYY-- I Know there is a some nice way to do this using XML SYNTAX
-- and without using the CONCATENATE FUNCTION
--The following work but gives a wierd column name.
--How can we rename the column name to something readable ?SELECT ',' + mm AS [text()]
FROM #t
FOR XML PATH('')
Try:
select stuff((select ',' + mm
from #t
for xml path(''),TYPE).value('.','varchar(max)'),1,1,'');
November 2, 2018 at 9:52 am
Folks.. No need to reply... All set, I just modified the code a bit , it works.. Allows you to group by ID
if object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;
Create table #t(mm VARCHAR(100), ID int )
INSERT INTO #t( mm, ID) VALUES ( 'ABC',1 );
INSERT INTO #t( mm, ID) VALUES ( 'DDD',2 );
INSERT INTO #t( mm, ID) VALUES ( 'XXX',2 );
INSERT INTO #t( mm, ID) VALUES ( 'YYY',1 );
--Instead of 4 rows I would like one line and the
--output should look like
--ABC,DDD,XXX,YYY
-- I Know there is a some nice way to do this using XML SYNTAX
-- and without using the CONCATENATE FUNCTION
--The following work but gives a wierd column name.
--How can we rename the column name to something readable ?
--SELECT ',' + mm AS [text()]
--FROM #t
--FOR XML PATH('')
SELECT ID, abc = STUFF(
(SELECT ',' + mm
FROM #t t1
WHERE t1.id = t2.id
FOR XML PATH (''))
, 1, 1, '') from #t t2
group by id;
November 4, 2018 at 4:53 pm
mw_sql_developer - Friday, November 2, 2018 9:52 AMFolks.. No need to reply... All set, I just modified the code a bit , it works.. Allows you to group by ID
if object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;
Create table #t(mm VARCHAR(100), ID int )
INSERT INTO #t( mm, ID) VALUES ( 'ABC',1 );
INSERT INTO #t( mm, ID) VALUES ( 'DDD',2 );
INSERT INTO #t( mm, ID) VALUES ( 'XXX',2 );
INSERT INTO #t( mm, ID) VALUES ( 'YYY',1 );--Instead of 4 rows I would like one line and the
--output should look like
--ABC,DDD,XXX,YYY-- I Know there is a some nice way to do this using XML SYNTAX
-- and without using the CONCATENATE FUNCTION
--The following work but gives a wierd column name.
--How can we rename the column name to something readable ?--SELECT ',' + mm AS [text()]
--FROM #t
--FOR XML PATH('')SELECT ID, abc = STUFF(
(SELECT ',' + mm
FROM #t t1
WHERE t1.id = t2.id
FOR XML PATH (''))
, 1, 1, '') from #t t2
group by id;
Heh... now wait for when an XML reserved character pops up in the data. 😉 You can overcome that with TYPE but I'll let you look that up.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 5, 2018 at 1:48 am
Lynn Pettis - Tuesday, September 18, 2018 11:22 AMmw_sql_developer - Tuesday, September 18, 2018 11:14 AMPlease run my code and you will see what I mean.
So I like to concatenate values in a column and get one row of output.
The solution below works.. But the column name it gives has a wierd guid.
How ca we chane that column name
if object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;Create table #t(mm VARCHAR(100) )
INSERT INTO #t( mm) VALUES ( 'ABC' );
INSERT INTO #t( mm) VALUES ( 'DDD' );
INSERT INTO #t( mm) VALUES ( 'XXX' );
INSERT INTO #t( mm) VALUES ( 'YYY' );Select * FROM #t;
--Instead of 4 rows I would like one line and the
--output should look like
--ABC,DDD,XXX,YYY-- I Know there is a some nice way to do this using XML SYNTAX
-- and without using the CONCATENATE FUNCTION
--The following work but gives a wierd column name.
--How can we rename the column name to something readable ?SELECT ',' + mm AS [text()]
FROM #t
FOR XML PATH('')Try:
select stuff((select ',' + mm
from #t
for xml path(''),TYPE).value('.','varchar(max)'),1,1,'');
Always use the text() function for this, prevents the reconstruct of the returned set as XML.
😎
select stuff((select ',' + mm
from #t
for xml path(''),TYPE).value('(./text())[1]','varchar(max)'),1,1,'');
November 5, 2018 at 9:00 am
Eirikur Eiriksson - Monday, November 5, 2018 1:48 AMLynn Pettis - Tuesday, September 18, 2018 11:22 AMmw_sql_developer - Tuesday, September 18, 2018 11:14 AMPlease run my code and you will see what I mean.
So I like to concatenate values in a column and get one row of output.
The solution below works.. But the column name it gives has a wierd guid.
How ca we chane that column name
if object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;Create table #t(mm VARCHAR(100) )
INSERT INTO #t( mm) VALUES ( 'ABC' );
INSERT INTO #t( mm) VALUES ( 'DDD' );
INSERT INTO #t( mm) VALUES ( 'XXX' );
INSERT INTO #t( mm) VALUES ( 'YYY' );Select * FROM #t;
--Instead of 4 rows I would like one line and the
--output should look like
--ABC,DDD,XXX,YYY-- I Know there is a some nice way to do this using XML SYNTAX
-- and without using the CONCATENATE FUNCTION
--The following work but gives a wierd column name.
--How can we rename the column name to something readable ?SELECT ',' + mm AS [text()]
FROM #t
FOR XML PATH('')Try:
select stuff((select ',' + mm
from #t
for xml path(''),TYPE).value('.','varchar(max)'),1,1,'');Always use the text() function for this, prevents the reconstruct of the returned set as XML.
😎
select stuff((select ',' + mm
from #t
for xml path(''),TYPE).value('(./text())[1]','varchar(max)'),1,1,'');
Lynn:
if you don;t mind can you help me with adding that TYPE to the following SQL please ?
SELECT ID, abc = STUFF(
(SELECT ',' + mm
FROM #t t1
WHERE t1.id = t2.id
FOR XML PATH (''))
, 1, 1, '') from #t t2
group by id;
November 5, 2018 at 9:05 am
mw_sql_developer - Monday, November 5, 2018 9:00 AMEirikur Eiriksson - Monday, November 5, 2018 1:48 AMLynn Pettis - Tuesday, September 18, 2018 11:22 AMmw_sql_developer - Tuesday, September 18, 2018 11:14 AMPlease run my code and you will see what I mean.
So I like to concatenate values in a column and get one row of output.
The solution below works.. But the column name it gives has a wierd guid.
How ca we chane that column name
if object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;Create table #t(mm VARCHAR(100) )
INSERT INTO #t( mm) VALUES ( 'ABC' );
INSERT INTO #t( mm) VALUES ( 'DDD' );
INSERT INTO #t( mm) VALUES ( 'XXX' );
INSERT INTO #t( mm) VALUES ( 'YYY' );Select * FROM #t;
--Instead of 4 rows I would like one line and the
--output should look like
--ABC,DDD,XXX,YYY-- I Know there is a some nice way to do this using XML SYNTAX
-- and without using the CONCATENATE FUNCTION
--The following work but gives a wierd column name.
--How can we rename the column name to something readable ?SELECT ',' + mm AS [text()]
FROM #t
FOR XML PATH('')Try:
select stuff((select ',' + mm
from #t
for xml path(''),TYPE).value('.','varchar(max)'),1,1,'');Always use the text() function for this, prevents the reconstruct of the returned set as XML.
😎
select stuff((select ',' + mm
from #t
for xml path(''),TYPE).value('(./text())[1]','varchar(max)'),1,1,'');
Lynn:
if you don;t mind can you help me with adding that TYPE to the following SQL please ?
SELECT ID, abc = STUFF(
(SELECT ',' + mm
FROM #t t1
WHERE t1.id = t2.id
FOR XML PATH (''))
, 1, 1, '') from #t t2
group by id;
Sorry , no need, I just figured out.. So is this the solution that Jeff M was talking about.....
So by using the TYPE you are safe dealing with data that has XML reserved characters
if object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;
Create table #t(mm VARCHAR(100), ID int )
INSERT INTO #t( mm, ID) VALUES ( 'ABC',1 );
INSERT INTO #t( mm, ID) VALUES ( 'DDD',2 );
INSERT INTO #t( mm, ID) VALUES ( 'XXX',2 );
INSERT INTO #t( mm, ID) VALUES ( 'YYY',1 );
SELECT ID, abc = STUFF(
(SELECT ',' + mm
FROM #t t1
WHERE t1.id = t2.id
FOR xml path(''),TYPE).value('(./text())[1]','varchar(max)'),1,1,'') from #t t2
group by id;
November 5, 2018 at 9:19 am
mw_sql_developer - Monday, November 5, 2018 9:05 AMmw_sql_developer - Monday, November 5, 2018 9:00 AMEirikur Eiriksson - Monday, November 5, 2018 1:48 AMLynn Pettis - Tuesday, September 18, 2018 11:22 AMmw_sql_developer - Tuesday, September 18, 2018 11:14 AMPlease run my code and you will see what I mean.
So I like to concatenate values in a column and get one row of output.
The solution below works.. But the column name it gives has a wierd guid.
How ca we chane that column name
if object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;Create table #t(mm VARCHAR(100) )
INSERT INTO #t( mm) VALUES ( 'ABC' );
INSERT INTO #t( mm) VALUES ( 'DDD' );
INSERT INTO #t( mm) VALUES ( 'XXX' );
INSERT INTO #t( mm) VALUES ( 'YYY' );Select * FROM #t;
--Instead of 4 rows I would like one line and the
--output should look like
--ABC,DDD,XXX,YYY-- I Know there is a some nice way to do this using XML SYNTAX
-- and without using the CONCATENATE FUNCTION
--The following work but gives a wierd column name.
--How can we rename the column name to something readable ?SELECT ',' + mm AS [text()]
FROM #t
FOR XML PATH('')Try:
select stuff((select ',' + mm
from #t
for xml path(''),TYPE).value('.','varchar(max)'),1,1,'');Always use the text() function for this, prevents the reconstruct of the returned set as XML.
😎
select stuff((select ',' + mm
from #t
for xml path(''),TYPE).value('(./text())[1]','varchar(max)'),1,1,'');
Lynn:
if you don;t mind can you help me with adding that TYPE to the following SQL please ?
SELECT ID, abc = STUFF(
(SELECT ',' + mm
FROM #t t1
WHERE t1.id = t2.id
FOR XML PATH (''))
, 1, 1, '') from #t t2
group by id;Sorry , no need, I just figured out.. So is this the solution that Jeff M was talking about.....
So by using the TYPE you are safe dealing with data that has XML reserved characters
if object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;
Create table #t(mm VARCHAR(100), ID int )
INSERT INTO #t( mm, ID) VALUES ( 'ABC',1 );
INSERT INTO #t( mm, ID) VALUES ( 'DDD',2 );
INSERT INTO #t( mm, ID) VALUES ( 'XXX',2 );
INSERT INTO #t( mm, ID) VALUES ( 'YYY',1 );SELECT ID, abc = STUFF(
(SELECT ',' + mm
FROM #t t1
WHERE t1.id = t2.id
FOR xml path(''),TYPE).value('(./text())[1]','varchar(max)'),1,1,'') from #t t2
group by id;
If object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;
Create table #t(mm VARCHAR(100), ID int )
INSERT INTO #t( mm, ID) VALUES ( 'AB<C',1 );
INSERT INTO #t( mm, ID) VALUES ( '<!=D>DD',2 );
INSERT INTO #t( mm, ID) VALUES ( 'XXX',2 );
INSERT INTO #t( mm, ID) VALUES ( 'YYY',1 );
--Handles XML reserved characters well!
SELECT ID, abc = STUFF(
(SELECT ',' + mm
FROM #t t1
WHERE t1.id = t2.id
FOR xml path(''),TYPE).value('(./text())[1]','varchar(max)'),1,1,'') from #t t2
group by id;
--Fails
SELECT ID, abc = STUFF(
(SELECT ',' + mm
FROM #t t1
WHERE t1.id = t2.id
FOR XML PATH (''))
, 1, 1, '') from #t t2
group by id;
November 5, 2018 at 9:38 am
mw_sql_developer - Monday, November 5, 2018 9:19 AMmw_sql_developer - Monday, November 5, 2018 9:05 AMmw_sql_developer - Monday, November 5, 2018 9:00 AMEirikur Eiriksson - Monday, November 5, 2018 1:48 AMLynn Pettis - Tuesday, September 18, 2018 11:22 AMmw_sql_developer - Tuesday, September 18, 2018 11:14 AMPlease run my code and you will see what I mean.
So I like to concatenate values in a column and get one row of output.
The solution below works.. But the column name it gives has a wierd guid.
How ca we chane that column name
if object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;Create table #t(mm VARCHAR(100) )
INSERT INTO #t( mm) VALUES ( 'ABC' );
INSERT INTO #t( mm) VALUES ( 'DDD' );
INSERT INTO #t( mm) VALUES ( 'XXX' );
INSERT INTO #t( mm) VALUES ( 'YYY' );Select * FROM #t;
--Instead of 4 rows I would like one line and the
--output should look like
--ABC,DDD,XXX,YYY-- I Know there is a some nice way to do this using XML SYNTAX
-- and without using the CONCATENATE FUNCTION
--The following work but gives a wierd column name.
--How can we rename the column name to something readable ?SELECT ',' + mm AS [text()]
FROM #t
FOR XML PATH('')Try:
select stuff((select ',' + mm
from #t
for xml path(''),TYPE).value('.','varchar(max)'),1,1,'');Always use the text() function for this, prevents the reconstruct of the returned set as XML.
😎
select stuff((select ',' + mm
from #t
for xml path(''),TYPE).value('(./text())[1]','varchar(max)'),1,1,'');
Lynn:
if you don;t mind can you help me with adding that TYPE to the following SQL please ?
SELECT ID, abc = STUFF(
(SELECT ',' + mm
FROM #t t1
WHERE t1.id = t2.id
FOR XML PATH (''))
, 1, 1, '') from #t t2
group by id;Sorry , no need, I just figured out.. So is this the solution that Jeff M was talking about.....
So by using the TYPE you are safe dealing with data that has XML reserved characters
if object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;
Create table #t(mm VARCHAR(100), ID int )
INSERT INTO #t( mm, ID) VALUES ( 'ABC',1 );
INSERT INTO #t( mm, ID) VALUES ( 'DDD',2 );
INSERT INTO #t( mm, ID) VALUES ( 'XXX',2 );
INSERT INTO #t( mm, ID) VALUES ( 'YYY',1 );SELECT ID, abc = STUFF(
(SELECT ',' + mm
FROM #t t1
WHERE t1.id = t2.id
FOR xml path(''),TYPE).value('(./text())[1]','varchar(max)'),1,1,'') from #t t2
group by id;
If object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;
Create table #t(mm VARCHAR(100), ID int )
INSERT INTO #t( mm, ID) VALUES ( 'AB<C',1 );
INSERT INTO #t( mm, ID) VALUES ( '<!=D>DD',2 );
INSERT INTO #t( mm, ID) VALUES ( 'XXX',2 );
INSERT INTO #t( mm, ID) VALUES ( 'YYY',1 );--Handles XML reserved characters well!
SELECT ID, abc = STUFF(
(SELECT ',' + mm
FROM #t t1
WHERE t1.id = t2.id
FOR xml path(''),TYPE).value('(./text())[1]','varchar(max)'),1,1,'') from #t t2
group by id;--Fails
SELECT ID, abc = STUFF(
(SELECT ',' + mm
FROM #t t1
WHERE t1.id = t2.id
FOR XML PATH (''))
, 1, 1, '') from #t t2
group by id;
What are you going on about? I've demonstrated how to do this.
😎
You need to understand how this works.
November 5, 2018 at 11:10 am
Eirikur Eiriksson - Monday, November 5, 2018 9:38 AMmw_sql_developer - Monday, November 5, 2018 9:19 AMmw_sql_developer - Monday, November 5, 2018 9:05 AMmw_sql_developer - Monday, November 5, 2018 9:00 AMEirikur Eiriksson - Monday, November 5, 2018 1:48 AMLynn Pettis - Tuesday, September 18, 2018 11:22 AMmw_sql_developer - Tuesday, September 18, 2018 11:14 AMPlease run my code and you will see what I mean.
So I like to concatenate values in a column and get one row of output.
The solution below works.. But the column name it gives has a wierd guid.
How ca we chane that column name
if object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;Create table #t(mm VARCHAR(100) )
INSERT INTO #t( mm) VALUES ( 'ABC' );
INSERT INTO #t( mm) VALUES ( 'DDD' );
INSERT INTO #t( mm) VALUES ( 'XXX' );
INSERT INTO #t( mm) VALUES ( 'YYY' );Select * FROM #t;
--Instead of 4 rows I would like one line and the
--output should look like
--ABC,DDD,XXX,YYY-- I Know there is a some nice way to do this using XML SYNTAX
-- and without using the CONCATENATE FUNCTION
--The following work but gives a wierd column name.
--How can we rename the column name to something readable ?SELECT ',' + mm AS [text()]
FROM #t
FOR XML PATH('')Try:
select stuff((select ',' + mm
from #t
for xml path(''),TYPE).value('.','varchar(max)'),1,1,'');Always use the text() function for this, prevents the reconstruct of the returned set as XML.
😎
select stuff((select ',' + mm
from #t
for xml path(''),TYPE).value('(./text())[1]','varchar(max)'),1,1,'');
Lynn:
if you don;t mind can you help me with adding that TYPE to the following SQL please ?
SELECT ID, abc = STUFF(
(SELECT ',' + mm
FROM #t t1
WHERE t1.id = t2.id
FOR XML PATH (''))
, 1, 1, '') from #t t2
group by id;Sorry , no need, I just figured out.. So is this the solution that Jeff M was talking about.....
So by using the TYPE you are safe dealing with data that has XML reserved characters
if object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;
Create table #t(mm VARCHAR(100), ID int )
INSERT INTO #t( mm, ID) VALUES ( 'ABC',1 );
INSERT INTO #t( mm, ID) VALUES ( 'DDD',2 );
INSERT INTO #t( mm, ID) VALUES ( 'XXX',2 );
INSERT INTO #t( mm, ID) VALUES ( 'YYY',1 );SELECT ID, abc = STUFF(
(SELECT ',' + mm
FROM #t t1
WHERE t1.id = t2.id
FOR xml path(''),TYPE).value('(./text())[1]','varchar(max)'),1,1,'') from #t t2
group by id;
If object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;
Create table #t(mm VARCHAR(100), ID int )
INSERT INTO #t( mm, ID) VALUES ( 'AB<C',1 );
INSERT INTO #t( mm, ID) VALUES ( '<!=D>DD',2 );
INSERT INTO #t( mm, ID) VALUES ( 'XXX',2 );
INSERT INTO #t( mm, ID) VALUES ( 'YYY',1 );--Handles XML reserved characters well!
SELECT ID, abc = STUFF(
(SELECT ',' + mm
FROM #t t1
WHERE t1.id = t2.id
FOR xml path(''),TYPE).value('(./text())[1]','varchar(max)'),1,1,'') from #t t2
group by id;--Fails
SELECT ID, abc = STUFF(
(SELECT ',' + mm
FROM #t t1
WHERE t1.id = t2.id
FOR XML PATH (''))
, 1, 1, '') from #t t2
group by id;What are you going on about? I've demonstrated how to do this.
😎You need to understand how this works.
Thank you for help Lynn, Yep, I understand after experimenting. Yes 😎
November 5, 2018 at 4:18 pm
mw_sql_developer - Monday, November 5, 2018 11:10 AMEirikur Eiriksson - Monday, November 5, 2018 9:38 AMmw_sql_developer - Monday, November 5, 2018 9:19 AMmw_sql_developer - Monday, November 5, 2018 9:05 AMmw_sql_developer - Monday, November 5, 2018 9:00 AMEirikur Eiriksson - Monday, November 5, 2018 1:48 AMLynn Pettis - Tuesday, September 18, 2018 11:22 AMmw_sql_developer - Tuesday, September 18, 2018 11:14 AMPlease run my code and you will see what I mean.
So I like to concatenate values in a column and get one row of output.
The solution below works.. But the column name it gives has a wierd guid.
How ca we chane that column name
if object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;Create table #t(mm VARCHAR(100) )
INSERT INTO #t( mm) VALUES ( 'ABC' );
INSERT INTO #t( mm) VALUES ( 'DDD' );
INSERT INTO #t( mm) VALUES ( 'XXX' );
INSERT INTO #t( mm) VALUES ( 'YYY' );Select * FROM #t;
--Instead of 4 rows I would like one line and the
--output should look like
--ABC,DDD,XXX,YYY-- I Know there is a some nice way to do this using XML SYNTAX
-- and without using the CONCATENATE FUNCTION
--The following work but gives a wierd column name.
--How can we rename the column name to something readable ?SELECT ',' + mm AS [text()]
FROM #t
FOR XML PATH('')Try:
select stuff((select ',' + mm
from #t
for xml path(''),TYPE).value('.','varchar(max)'),1,1,'');Always use the text() function for this, prevents the reconstruct of the returned set as XML.
😎
select stuff((select ',' + mm
from #t
for xml path(''),TYPE).value('(./text())[1]','varchar(max)'),1,1,'');
Lynn:
if you don;t mind can you help me with adding that TYPE to the following SQL please ?
SELECT ID, abc = STUFF(
(SELECT ',' + mm
FROM #t t1
WHERE t1.id = t2.id
FOR XML PATH (''))
, 1, 1, '') from #t t2
group by id;Sorry , no need, I just figured out.. So is this the solution that Jeff M was talking about.....
So by using the TYPE you are safe dealing with data that has XML reserved characters
if object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;
Create table #t(mm VARCHAR(100), ID int )
INSERT INTO #t( mm, ID) VALUES ( 'ABC',1 );
INSERT INTO #t( mm, ID) VALUES ( 'DDD',2 );
INSERT INTO #t( mm, ID) VALUES ( 'XXX',2 );
INSERT INTO #t( mm, ID) VALUES ( 'YYY',1 );SELECT ID, abc = STUFF(
(SELECT ',' + mm
FROM #t t1
WHERE t1.id = t2.id
FOR xml path(''),TYPE).value('(./text())[1]','varchar(max)'),1,1,'') from #t t2
group by id;
If object_id('tempdb..#t') IS NOT NULL DROP TABLE #t;
Create table #t(mm VARCHAR(100), ID int )
INSERT INTO #t( mm, ID) VALUES ( 'AB<C',1 );
INSERT INTO #t( mm, ID) VALUES ( '<!=D>DD',2 );
INSERT INTO #t( mm, ID) VALUES ( 'XXX',2 );
INSERT INTO #t( mm, ID) VALUES ( 'YYY',1 );--Handles XML reserved characters well!
SELECT ID, abc = STUFF(
(SELECT ',' + mm
FROM #t t1
WHERE t1.id = t2.id
FOR xml path(''),TYPE).value('(./text())[1]','varchar(max)'),1,1,'') from #t t2
group by id;--Fails
SELECT ID, abc = STUFF(
(SELECT ',' + mm
FROM #t t1
WHERE t1.id = t2.id
FOR XML PATH (''))
, 1, 1, '') from #t t2
group by id;What are you going on about? I've demonstrated how to do this.
😎You need to understand how this works.
Thank you for help Lynn, Yep, I understand after experimenting. Yes 😎
You are welcome, but it wasn't me that helped you with adding ,TYPE to your code.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply