sql – 从DB2中的查询创建分隔字符串

栏目: 数据库 · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/3728010/create-a-delimitted-string-from-a-query-in-db2

我试图从iSeries(AS / 400)上的DB2中的查询结果创建一个分隔字符串.我在T-SQL中完成了这个,但是在这里找不到办法.

这是我在T-SQL中的代码.我正在寻找DB2中的equivelant.

DECLARE @a VARCHAR(1000)
SELECT @a = COALESCE(@a + ', ' + [Description], [Description])
FROM AP.Checkbooks
SELECT @a

如果我表中的描述如下所示:

Desc 1  Desc 2  Desc 3

然后它会返回:

Desc 1, Desc 2, Desc 3
基本上,您正在寻找DB2中的 MySQL 的GROUP_CONCAT聚合函数.根据 one thread I found

,您可以通过XMLAGG函数模仿此行为:

create table t1 (num int, color varchar(10));

insert into t1 values (1,'red'), (1,'black'), (2,'red'), (2,'yellow'), (2,'green');

select num,
  substr( xmlserialize( xmlagg( xmltext( concat( ', ', color ) ) ) as varchar( 1024 ) ), 3 )
  from t1
  group by num;

这会回来

1 red,black
2 red,yellow,green

(或者,如果我正确地阅读了东西)

翻译自:https://stackoverflow.com/questions/3728010/create-a-delimitted-string-from-a-query-in-db2


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Distributed Algorithms

Distributed Algorithms

Wan Fokkink / The MIT Press / 2013-12-6 / USD 40.00

This book offers students and researchers a guide to distributed algorithms that emphasizes examples and exercises rather than the intricacies of mathematical models. It avoids mathematical argumentat......一起来看看 《Distributed Algorithms》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具