SterlinkArcher
SterlinkArcher

Reputation: 731

Displaying multiple values from same column in one row in SQL

I'm working on a Stored procedure, to extend it with one function here on my job. At the moment I have a problem in displaying multiple values from the same column in one row, and they have to be differed and presented like this 25,26,27

So this is what I've been trying.

DECLARE @myVariable varhcar(200) null)
SELECT @myVariable = COALESCE(@myVariable + '','','') + stringvalue
from TABLE
WHERE COLUMN1 = SOMEVARIABLE
and ISSUE = COLUMN2

SELECT @Headtext = 'name' + convert(varchar, @myVariable)

Before this in the SP I create a table, into which it displays other data. I want the SP to create rows with this data aswell. Still got some troubles not sure have to this, first timer with this kind of SP.

Upvotes: 0

Views: 3758

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270733

Your code should look like:

DECLARE @myVariable varhcar(200);
SELECT @myVariable = COALESCE(@myVariable + ',', '') + stringvalue
from TABLE
WHERE COLUMN1 = SOMEVARIABLE and ISSUE = COLUMN2;

SELECT @Headtext = 'name' + @myVariable;

Another way to concatenate the variables is:

SELECT @myVariable = stuff((select ',' + stringvalue
                            from TABLE
                            WHERE COLUMN1 = SOMEVARIABLE and ISSUE = COLUMN2
                            for xml path ('')
                           ), 1, 1, '');

SELECT @Headtext = 'name' + @myVariable;

Upvotes: 2

Related Questions