?
Solved

Mysql equivalent of 'group_concat()' in SQL server

Posted on 2005-03-30
7
Medium Priority
?
7,890 Views
Last Modified: 2010-04-08
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.
0
Comment
Question by:nagki
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 13661511
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
 
LVL 8

Author Comment

by:nagki
ID: 13687017
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
 
LVL 51

Accepted Solution

by:
Vitor Montalvão earned 1000 total points
ID: 13695911
Then see if this works:

DECLARE @MyVar AS VARCHAR(4000)

SELECT @MyVar = @MyVar + FieldName
FROM TableName
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Author Comment

by:nagki
ID: 13725558
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
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 13725582
I think that you can't! :-|
And I am affraid that the solution is something you don't want to do (stored procedure).
0
 
LVL 8

Author Comment

by:nagki
ID: 13760617
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
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 13760645
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

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question