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
SQL Server equivalent of WM_CONCAT function
提问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 APPLY
and FOR XML PATH('')
). example,
您没有相应的功能,但您仍然可以模拟(使用CROSS APPLY
和FOR 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)