Darknight Darknight - 7 months ago 8
SQL Question

SQL, How to Concatenate results?

I currently have a SQL query that returns a number of fields. I need one f the fields to be effectively a sub query sub that.

The Problem in detail:


If I have a table X with two columns, ModuleID and say ModuleValue, how can I write a SQL query to take the results and Concatenate it into one field:


EG Results returned from

(SELECT ModuleValue FROM Table_X WHERE ModuleID=@ModuleID)


Value 1

Value 2

Value 3

...

I need to return the result thus (as a single row, unlike the above):


Value 1, Value 2, Value 3


Is there a simple Concatenation method that could be user?

EDIT:


DB is MS TSQL (2005)

Answer

With MSSQL you can do something like this:

declare @result varchar(500)
set @result = ''
select @result = @result + ModuleValue + ', ' 
from TableX where ModuleId = @ModuleId
Comments