Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I think a lot of people solve this with the STUFF function. I found an example from this linkthis link and adapted it.

SET NOCOUNT ON

DECLARE @Table TABLE (StringText VARCHAR(100))
DECLARE @ConcatString VARCHAR(100)

INSERT INTO @Table VALUES ('Tom')
INSERT INTO @Table VALUES ('Dick')
INSERT INTO @Table VALUES ('Harry')

SELECT @ConcatString = STUFF((
            SELECT ',' + StringText
            FROM @Table
            ORDER BY StringText
            FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'),1,1,'')

PRINT @ConcatString

As Dan Guzman points out in the comments, specifying TYPE and value method avoids entity references in the resultant string for special characters like ampersand.

I think a lot of people solve this with the STUFF function. I found an example from this link and adapted it.

SET NOCOUNT ON

DECLARE @Table TABLE (StringText VARCHAR(100))
DECLARE @ConcatString VARCHAR(100)

INSERT INTO @Table VALUES ('Tom')
INSERT INTO @Table VALUES ('Dick')
INSERT INTO @Table VALUES ('Harry')

SELECT @ConcatString = STUFF((
            SELECT ',' + StringText
            FROM @Table
            ORDER BY StringText
            FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'),1,1,'')

PRINT @ConcatString

As Dan Guzman points out in the comments, specifying TYPE and value method avoids entity references in the resultant string for special characters like ampersand.

I think a lot of people solve this with the STUFF function. I found an example from this link and adapted it.

SET NOCOUNT ON

DECLARE @Table TABLE (StringText VARCHAR(100))
DECLARE @ConcatString VARCHAR(100)

INSERT INTO @Table VALUES ('Tom')
INSERT INTO @Table VALUES ('Dick')
INSERT INTO @Table VALUES ('Harry')

SELECT @ConcatString = STUFF((
            SELECT ',' + StringText
            FROM @Table
            ORDER BY StringText
            FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'),1,1,'')

PRINT @ConcatString

As Dan Guzman points out in the comments, specifying TYPE and value method avoids entity references in the resultant string for special characters like ampersand.

added 170 characters in body
Source Link

I think a lot of people solve this with the STUFF commandSTUFF function. I found an example from this link and adapted it.

SetSET nocountNOCOUNT onON
Declare
DECLARE @Table tableTABLE (StringText varcharVARCHAR(100))
DECLARE @ConcatString varcharVARCHAR(100)

insertINSERT intoINTO @Table valuesVALUES ('Tom')
insertINSERT intoINTO @Table valuesVALUES ('Dick')
insertINSERT intoINTO @Table valuesVALUES ('Harry')
SELECT 
  SELECT @ConcatString = STUFF( (SELECT ',' + StringText
              SELECT ',' + StringText
            FROM @Table 
                             ORDER BY StringText
                             FOR XML PATH('')), 
                           TYPE).value('.', 'VARCHAR(MAX)'),1, 1, '')
print
PRINT @ConcatString

As Dan Guzman points out in the comments, specifying TYPE and value method avoids entity references in the resultant string for special characters like ampersand.

I think a lot of people solve this with the STUFF command. I found an example from this link and adapted it.

Set nocount on
Declare @Table table (StringText varchar(100))
DECLARE @ConcatString varchar(100)

insert into @Table values('Tom')
insert into @Table values('Dick')
insert into @Table values('Harry')
SELECT 
   @ConcatString = STUFF( (SELECT ',' + StringText
                             FROM @Table 
                             ORDER BY StringText
                             FOR XML PATH('')), 
                            1, 1, '')
print @ConcatString

I think a lot of people solve this with the STUFF function. I found an example from this link and adapted it.

SET NOCOUNT ON

DECLARE @Table TABLE (StringText VARCHAR(100))
DECLARE @ConcatString VARCHAR(100)

INSERT INTO @Table VALUES ('Tom')
INSERT INTO @Table VALUES ('Dick')
INSERT INTO @Table VALUES ('Harry')

SELECT @ConcatString = STUFF((
            SELECT ',' + StringText
            FROM @Table
            ORDER BY StringText
            FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'),1,1,'')

PRINT @ConcatString

As Dan Guzman points out in the comments, specifying TYPE and value method avoids entity references in the resultant string for special characters like ampersand.

added 213 characters in body
Source Link

I think a lot of people solve this with the STUFF command. I found the followingan example from this link and adapted it.

Set nocount on
Declare @Table table (StringText varchar(100))
DECLARE @CodeNameString@ConcatString varchar(100)

insert into @Table values('Tom')
insert into @Table values('Dick')
insert into @Table values('Harry')
SELECT 
   @CodeNameString@ConcatString = STUFF( (SELECT ',' + CodeName StringText
                             FROM dbo.AccountCodes@Table 
                             ORDER BY SortStringText
                             FOR XML PATH('')), 
                            1, 1, '')
print @ConcatString

I think a lot of people solve this with the STUFF command. I found the following example from this link

DECLARE @CodeNameString varchar(100)

SELECT 
   @CodeNameString = STUFF( (SELECT ',' + CodeName 
                             FROM dbo.AccountCodes 
                             ORDER BY Sort
                             FOR XML PATH('')), 
                            1, 1, '')

I think a lot of people solve this with the STUFF command. I found an example from this link and adapted it.

Set nocount on
Declare @Table table (StringText varchar(100))
DECLARE @ConcatString varchar(100)

insert into @Table values('Tom')
insert into @Table values('Dick')
insert into @Table values('Harry')
SELECT 
   @ConcatString = STUFF( (SELECT ',' + StringText
                             FROM @Table 
                             ORDER BY StringText
                             FOR XML PATH('')), 
                            1, 1, '')
print @ConcatString
Source Link
Loading