Mysql equivalent of 'group_concat()' in SQL server

In mysql,

using group_concat()..we can concatenate column values from different rows..

Is there any equivalent function in SQL server..

I am not interested in stored procedures/functions.
LVL 8
nagkiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
As far I know, the answer is no!
Follow this example to see if it can help you:

DECLARE @MyVar AS VARCHAR(4000)

SELECT @MyVar = @MyVar + Field1 + Field2
FROM TableName

0
nagkiAuthor Commented:
Hi VMontalvao,

Thanx for ur quick reply..
But the example u specified won't help for my case...

using your example i can concatenate two columns in the same row..
But i want to concatenate same column in different rows..

Any final toughts?
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Then see if this works:

DECLARE @MyVar AS VARCHAR(4000)

SELECT @MyVar = @MyVar + FieldName
FROM TableName
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

nagkiAuthor Commented:
Hi VMontalvo..

sorry for late reply,

I added one more filed to the SQL that u suggested:

DECLARE @MyVar AS VARCHAR(4000)
SELECT @MyVar = @MyVar + user_id, unit_id
FROM dbo.users

But it has given this error:
A SELECT statement that assigns a value to a variable
must not be combined with data-retrieval operations.

How to get rid of that error?


0
Vitor MontalvãoMSSQL Senior EngineerCommented:
I think that you can't! :-|
And I am affraid that the solution is something you don't want to do (stored procedure).
0
nagkiAuthor Commented:
Hi..

Even in oracle u can achieve it by using analytical functions PARTITION BY..
i can't think why there is no function in SQL Server for doing that..

ok.. can u give me a stored procedure which will do that?
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
I don't know if in MySQL is equal to SQL Server, but on SQL Server just create a stored procedure that open a cursor and for each row concatenate the columns that you want.

Sorry that I don't have time to post here an example code, but check for these commands:
OPEN CURSOR
FETCH
CLOSE
DEALLOCATE

If you need more specific help, just tell and I will see if later I'll have some time to help you.

Good luck
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.