comma delimited list as a single string, T-SQL
本问题已经有最佳答案,请猛点这里访问。
我的T-SQL查询生成以下结果集
1 2 3 4 5 | ID Date 756 2011-08-29 756 2011-08-31 756 2011-09-01 756 2011-09-02 |
我怎么能像这样转换
1 2 | ID Date 756 2011-08-29, 2011-08-31, 2011-09-01, 2011-09-02 |
任何建议都将不胜感激。
您可以使用
1 2 3 4 5 6 7 8 | select distinct t1.id, STUFF( (SELECT ', ' + convert(varchar(10), t2.date, 120) FROM yourtable t2 where t1.id = t2.id FOR XML PATH ('')) , 1, 1, '') AS date from yourtable t1; |
参见SQL摆弄演示