SQL, How to Concatenate results? SQL, How to Concatenate results? sql sql

SQL, How to Concatenate results?


This one automatically excludes the trailing comma, unlike most of the other answers.

DECLARE @csv VARCHAR(1000)SELECT @csv = COALESCE(@csv + ',', '') + ModuleValueFROM Table_XWHERE ModuleID = @ModuleID

(If the ModuleValue column isn't already a string type then you might need to cast it to a VARCHAR.)


With MSSQL you can do something like this:

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


In mysql you'd use the following function:

SELECT GROUP_CONCAT(ModuleValue, ",") FROM Table_X WHERE ModuleID=@ModuleID

I am not sure which dialect you are using.