连接字符串,基于Case语句

时间:2015-12-03 20:56:09

标签: sql db2

我需要在每日报告中附加注释,其中注释根据值存储在不同的表中。

即。这些注释存储在注释表中,多个记录上,一个LineNo列,其值为1,2,3等,基于Notes中的记录数。 备注行数可变

我想选择Notes并在输出中将这3个字段连接在一起作为单个列;

即:

Select (Concat Notes Line1, Notes Line2 Notes Line3), 
       UserName, 
       NotesDate
From   notes 
Where  NoteType=2 
and    RefNo in (SELECT RefNo from Report);

我使用Case和Concat查看了各种解决方案,但似乎都没有。存储过程中已有一个Join来创建报告。

1 个答案:

答案 0 :(得分:0)

以下内容应该有效:

Select Line1 || Line2 || Line3, UserName, NotesDate From notes Where NoteType=2 and RefNo in (SELECT RefNo from Report)

如果你坚持使用concat命令(功能上等于||),则以下方法可行:

Select concat(concat(Line1,Line2),Line3), UserName, NotesDate From notes Where NoteType=2 and RefNo in (SELECT RefNo from Report)

DB2中的concat()命令只能处理两个参数,这就是需要嵌套的concat()的原因。

相关问题