Implode type function in SQL Server 2000?

后端 未结 2 1328
时光说笑
时光说笑 2020-12-10 19:00

Is there an Implode type function for SQL Server?

What I have is a list (in a SQL server table):

 Apple
 Orange
 Pear
 Blueberry

a

2条回答
  •  囚心锁ツ
    2020-12-10 19:32

    I typically use the FOR XML PATH method for this, it works for row subqueries as well, simply

    SELECT ', ' + 
    FROM 
    ORDER BY 
    FOR XML PATH('')
    

    This will give you your list with a ", " at the start, and the quickest way to remove that
    is to use stuff

    SELECT STUFF((
            SELECT ', ' + 
            FROM 
            ORDER BY 
            FOR XML PATH('')
        ),1,2,'')
    

提交回复
热议问题