oracle 相当于 WM_CONCAT 函数的 SQL Server

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12559551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:07:21  来源:igfitidea点击:

SQL Server equivalent of WM_CONCAT function

sqldatabaseoraclesql-server-2008tsql

提问by RGO

Possible Duplicate:
Concat field value to string in SQL Server

可能的重复:将
字段值连接到 SQL Server 中的字符串

What is the SQL Server equivalent of WM_CONCAT?

WM_CONCAT 的 SQL Server 等效项是什么?

回答by John Woo

You don't have an equivalent function for that, but you can still simulate (make useof CROSS APPLYand FOR XML PATH('')). example,

您没有相应的功能,但您仍然可以模拟(使用CROSS APPLYFOR XML PATH(''))。例子,

USERID  ADDRESSLINE1
==========================
1       First Street
1       Second Street
2       32th Street
2       24th Street
2       25th Street

will result

将导致

USERID  ADDRESSLIST
============================
1       First Street, Second Street
2       32th Street, 24th Street, 25th Street

Using this query:

使用此查询:

SELECT  a.UserID, 
        SUBSTRING(d.Addresses,1, LEN(d.Addresses) - 1) AddressList
FROM
        (
            SELECT DISTINCT UserID
            FROM tableName
        ) a
        CROSS APPLY
        (
            SELECT [AddressLine1] + ', ' 
            FROM tableName AS B 
            WHERE A.UserID = B.UserID 
            FOR XML PATH('')
        ) D (Addresses) 

SQLFiddle Demo

SQLFiddle 演示