Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Mysql equivalent of 'group_concat()' in SQL server

Posted on 2005-03-30
7
Medium Priority
?
7,952 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
  • 4
  • 3
7 Comments
 
LVL 53

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 53

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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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 53

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 53

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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.
Suggested Courses

577 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