2017 年之前 SQL Server 的 String

编程入门 行业动态 更新时间:2024-10-25 12:23:39
本文介绍了2017 年之前 SQL Server 的 String_agg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

谁能帮我使这个查询适用于 SQL Server 2014?

Can anyone help me make this query work for SQL Server 2014?

这适用于 Postgresql,可能适用于 SQL Server 2017.在 Oracle 上,它是 listagg 而不是 string_agg.

This is working on Postgresql and probably on SQL Server 2017. On Oracle it is listagg instead of string_agg.

这是SQL:

select string_agg(t.id,',') AS id from Table t

我在网站上检查了一些应该使用的 xml 选项,但我无法理解.

I checked on the site some xml option should be used but I could not understand it.

推荐答案

在 SQL Server 2017 之前,您可以:

In SQL Server pre-2017, you can do:

select stuff( (select ',' + cast(t.id as varchar(max)) from tabel t for xml path ('') ), 1, 1, '' );

stuff() 的唯一目的是去除开头的逗号.工作由for xml path完成.

The only purpose of stuff() is to remove the initial comma. The work is being done by for xml path.

更多推荐

2017 年之前 SQL Server 的 String

本文发布于:2023-10-17 07:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1500126.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SQL   String   Server

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!