8

I need a similar function to Oracle WM_CONCAT in SQL Server, which returns a comma separated list of whatever field you pass it as argument. For example, in Oracle,

select WM_CONCAT(first_name) from employee where state='CA' 

returns "John, Jim, Bob".

How can I do this in SQL Server?

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Areca
  • 1,292
  • 4
  • 11
  • 21

6 Answers6

4

In SQL Server 2017 STRING_AGG function has been added

SELECT t.name as TableName
      ,STRING_AGG(c.name, ';') AS FieldList
  FROM sys.tables t
  JOIN sys.columns c 
    ON t.object_id = c.object_id
  GROUP BY t.name;
schlebe
  • 3,387
  • 5
  • 37
  • 50
3

The actual answer:

SELECT
   SUBSTRING(buzz, 2, 2000000000)
FROM
    (
    SELECT 
        firstname
    FROM 
        employee
    WHERE
        State = 'CA'
    FOR XML PATH (',')
    ) fizz(buzz)

A common question here. Some searches:

Community
  • 1
  • 1
gbn
  • 422,506
  • 82
  • 585
  • 676
  • 3
    Worth noting this is only valid for SQL Server 2005 and later. OP hasn't stated which version of SQL Server they're using. – Chris J Oct 25 '09 at 20:14
  • 1
    You can replace the SUBSTRING, with the STUFF command which will function properly without needing to know the string length. – WesleyJohnson Oct 26 '09 at 00:53
  • 1
    @Chris J: I would say that SQL 2k5 is a reasonable assumption that 10 years after SQL 2000 RTM and 2.5 versions later – gbn Oct 26 '09 at 05:07
  • 1
    2147483647 is your friend... Memorize it. – ErikE Oct 04 '11 at 20:01
2

Try this:


    drop table #mike_temp 
go

select * into #mike_temp 
  from (select 'Ken'  as firstname, 'CO' as state
         union all
        select 'Mike' as firstname, 'CO' as state
         union all
        select 'Tom' as firstname , 'WY' as state
       ) a
go

SELECT distinct 
       state
      ,STUFF((SELECT ', ' + b.firstname FROM #mike_temp b where a.state = b.state FOR XML PATH('')),1, 2, '') AS CSVColumn
  from #mike_temp a
Mike C
  • 21
  • 2
0
 SELECT Field1, Substring(Field2, 2, LEN(Field2)) AS Field2 FROM
(
    SELECT
        [InnerData].Field1,
        (SELECT  ',' + Field2 FROM @Fields WHERE Field1=[InnerData].Field1 FOR XML PATH('')) AS Field2
        FROM
        (
            SELECT DISTINCT Field1 FROM @Fields
        ) AS [InnerData]
) AS OuterData

I got this Query from this Link

Refer this Link for more Clarification

Prince Antony G
  • 932
  • 4
  • 18
  • 39
0

AFAIK, you need to do it by yourself.

You could build an user defined function that loop with a cursor the records of Employee where the state is CA and returns the concatenation of their names.

eKek0
  • 23,005
  • 25
  • 91
  • 119
-3

try this

 select    
    wm_concat(name) 
 from
    employee
 where
    state='CA'
 group by
    state
Duy Khanh
  • 384
  • 1
  • 5
  • 11
  • This isn't correct. It doesn't work in SQL Server, as the OP pointed out. You might wish to delete it, or edit it if you meant something different? – criticalfix May 29 '13 at 14:07