使用逗号SQL Server将整型列表转换为字符串
如何在SQL Server中使用函数将int列表转换为带逗号的字符串 使用转换键转换为:“68,74,58,64,67” rextester演示: 返回:使用逗号SQL Server将整型列表转换为字符串,sql,sql-server,function,tsql,Sql,Sql Server,Function,Tsql,如何在SQL Server中使用函数将int列表转换为带逗号的字符串 使用转换键转换为:“68,74,58,64,67” rextester演示: 返回: +----------------+ | ProductIds | +----------------+ | 68,74,58,64,67 | +----------------+ 编辑:在SQL Server 2017+中,您可以使用string_agg(),根据Jeffry Schwartz的文章:使用 rextester演示
+----------------+
| ProductIds |
+----------------+
| 68,74,58,64,67 |
+----------------+
编辑:在SQL Server 2017+中,您可以使用string_agg()
,根据Jeffry Schwartz的文章:使用
rextester演示:
返回:
+----------------+
| ProductIds |
+----------------+
| 68,74,58,64,67 |
+----------------+
编辑:在SQL Server 2017+中,您可以使用
string_agg()
,根据Jeffry Schwartz的文章,性能似乎是相同的:您可以使用stuff和xml路径来连接和选择第一个逗号后的数据
select distinct stuff((select ',' + convert(char(2), productid) from #yourproductid for xml path('')),1,1, '')
from #yourproductid
您的桌子:
create table #yourproductid(productid int)
insert into #yourproductid (productid) values (68),(74),(58),(64),(67)
您可以使用stuff和xml路径来连接和选择第一个逗号之后的数据
select distinct stuff((select ',' + convert(char(2), productid) from #yourproductid for xml path('')),1,1, '')
from #yourproductid
您的桌子:
create table #yourproductid(productid int)
insert into #yourproductid (productid) values (68),(74),(58),(64),(67)
谷歌:“sql server聚合字符串concat”谷歌:“sql server聚合字符串concat”观看DVD上的入侵者Zim。。。大量的叫喊声。@johncapelletti我通过保持sql小写来平衡。^观看DVD上的入侵者Zim。。。大量的叫喊声。@johncapelletti我通过保留sql小写字母来平衡它^