Solved

wm_concat for sqlserver

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

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

809 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