Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用逗号SQL Server将整型列表转换为字符串_Sql_Sql Server_Function_Tsql - Fatal编程技术网

使用逗号SQL Server将整型列表转换为字符串

使用逗号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演示

如何在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的文章,性能似乎是相同的:

您可以使用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小写字母来平衡它^