0

I know how to concact two strings, but this one will be a bit more complicated, i guess it's a duplicate but i could not find it after an hour of search so....

Suppose i have a table like this :

ExempleTable
____________________
  ID   |  TEXT      |
___________________ |
 1     |  'hello '  |
 2     |  'how are '|
 3     |  'you ?'   |

I need a select that would return a single line containing :

'hello how are you ?'

I thought something like this would exist :

CONCATTEXT( SELECT text FROM ExampleTable) as sentence

But no... maybe it's a GROUP BY matter ? I can't figure it out...

Antoine Pelletier
  • 3,164
  • 3
  • 40
  • 62

2 Answers2

1
Declare @YourTable table (ID int,TEXT varchar(50))
 Insert Into @YourTable values
 (1,'hello '),
 (2,'how are '),
 (3,'you ?')

Select Stuff((Select ' '+ltrim(rtrim(Text)) From @YourTable Order by ID For XML Path ('')),1,1,'')    

Returns

hello how are you ?
John Cappelletti
  • 79,615
  • 7
  • 44
  • 66
0

You could use a variable:

declare @result varchar(max) = ''

select @result += field + ' ' from T

select rtrim(@result)
Alex K.
  • 171,639
  • 30
  • 264
  • 288