Solved

wm_concat for sqlserver

Posted on 2011-02-23
9
4,428 Views
Last Modified: 2012-05-11
What is the equivalent of the oracle wm_concat function for SqlServer 2005+?
0
Comment
Question by:rmtdev2
[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
9 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 34958982
0
 
LVL 10

Expert Comment

by:Asim Nazir
ID: 34959052
0
 
LVL 8

Expert Comment

by:vinurajr
ID: 34959146
Is wm_concat does a comma separate delimited string..? this can be done in sql server using COALESCE

just give a search for COALESCE you can find the examples.
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:rmtdev2
ID: 34966610
SELECT
   SUBSTRING(buzz, 2, 2000000000)
FROM
    (
    SELECT 
        rsp_id
    FROM 
        caproductrsphrase
    WHERE
        i_id = 1
    FOR XML PATH (',')
    ) fizz(buzz)

Open in new window


I tried the code above and got and error: Row name ',' contains an invalid XML identifier as required by FOR XML; ','(0x002C) is the first character at fault.

When I try without the comma my result is embedded with xml tags:
rsp_id>48</rsp_id><rsp_id>78</rsp_id><rsp_id>144</rsp_id><rsp_id>151</rsp_id><rsp_id>181</rsp_id><rsp_id>202</rsp_id>

Could you tell me where I went wrong?

Thanks
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 34967035
I suspect this is what you mean:

I will leave it up to you to decide whether you want the trailing comma and how to lose it if you do not want it.
SELECT
   buzz
FROM
    (
    SELECT 
        rsp_id + ', ' + Data()
    FROM 
        caproductrsphrase
    WHERE
        i_id = 1
    FOR XML PATH (',')
    ) fizz(buzz)

If rsp_id is numeric, instead of:
rsp_id + ', ' + Data()
Use:
CAST(rsp_id As varchar(20)) + ', ' + Data()

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 34967040
Oops there is a typo it should be:
SELECT
   buzz
FROM
    (
    SELECT 
        rsp_id + ', ' + Data()
    FROM 
        caproductrsphrase
    WHERE
        i_id = 1
    FOR XML PATH ('')
    ) fizz(buzz)

If rsp_id is numeric, instead of:
rsp_id + ', ' + Data()
Use:
CAST(rsp_id As varchar(20)) + ', ' + Data()

Open in new window

0
 

Accepted Solution

by:
rmtdev2 earned 0 total points
ID: 34967618
thanks, but I got "Data is not a recognized built in function name."

I accomplish it via the following:

SELECT p1.i_id,
       ( SELECT p3.rsp_code + ','
           FROM caproductrsphrase p2, carsphrase p3
          WHERE p2.i_id = p1.i_id
          and p2.rsp_id = p3.rsp_id
          ORDER BY p3.rsp_code
            FOR XML PATH('') ) AS rsp_codes
      FROM caproductrsphrase p1
      GROUP BY i_id

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 34976284
>>I got "Data is not a recognized built in function name."<<
Odd, as it worked fine for me.  Perhaps you are still using SQL Server 2005.
0
 

Author Closing Comment

by:rmtdev2
ID: 34995598
I found that this was the cleanest way
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

733 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