cnblogs/dcrenl/SQL 将列转成字符串并用逗号分隔(1).html
2024-09-24 12:43:01 +08:00

17 lines
1.1 KiB
HTML

<div id="sina_keyword_ad_area2" class="articalContent ">
<pre style="line-height:24px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-family:Arial; white-space:pre-wrap; word-wrap:break-word; zoom:1">
<span style="background-color:rgb(253,255,248)">SELECT STUFF((SELECT ',' &#43; FieldName FROM TableName FOR XML PATH('')),1,1,'') AS T</span>
</pre>
<pre style="line-height:24px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-family:Arial; white-space:pre-wrap; word-wrap:break-word; zoom:1">
其中的逗号可以换成其它字符
</pre>
<pre style="line-height:24px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-family:Arial; white-space:pre-wrap; word-wrap:break-word; zoom:1">
转换完成后的结果如下:
</pre>
<pre style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; word-wrap:break-word; zoom:1">
<span style="white-space:pre-wrap">111,222,333,444,555,666</span>
</pre>
</div>