Home > database >  Content of SQL string concatenation
Content of SQL string concatenation

Time:11-01

Select * from tb1 where id='2'

Get the following table
Id text
2 a
2 c
2 d
2 b

How to connect the content of the text into a table, into the following

Id text
2 a b c d

CodePudding user response:

SELECT id,
Text=stuff ((
SELECT "+ text
The FROM tb1 t
WHERE t.i d=test2. Id
FOR XML path (' ')), 1, 1, ' ')
The FROM tb1
GROUP BY id.

CodePudding user response:

On the first floor of the changes a little bit about the
 SELECT id, 
Text=stuff ((
SELECT "+ text
The FROM tb1 t
WHERE t.i d=tb1. Id
FOR XML path (' ')), 1, 1, ' ')
The FROM tb1
WHERE id='2'
GROUP BY id.

CodePudding user response:

You this is a string of aggregation operation sqlserver2012, please use the aggregate function usage string_AGG strings
  • Related