473,748 Members | 2,891 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I concatenate strings from a column into a single row in DB2

How do I concatenate strings from a column into a single row?

Whats the logic to create the function in DB2.

Given is below.

Color
------
red
orange
blue
green

And return a resultset like this:

Colors
-------------------------
red,orange,blue ,green
Nov 19 '08 #1
6 13555
On Nov 19, 2:28*pm, mark83anth...@g mail.com wrote:
How do I concatenate strings from a column into a single row?

Whats the logic to create the function in DB2.

Given is below.

Color
------
red
orange
blue
green

And return a resultset like this:

Colors
-------------------------
red,orange,blue ,green
I will make it clear.

The table name is Colors
CREATE TABLE Colors
(
Color VARCHAR(32)
)

Values in the Table are
Color
------
red
orange
blue
green
My output should be when I do a Select * from Tabname, shd be

red,orange,blue ,green
Waiting for your reply

Thanks in Advancd
Nov 19 '08 #2
On Nov 19, 12:49*pm, mark83anth...@g mail.com wrote:
On Nov 19, 2:28*pm, mark83anth...@g mail.com wrote:
How do I concatenate strings from a column into a single row?
Whats the logic to create the function in DB2.
Given is below.
Color
------
red
orange
blue
green
And return a resultset like this:
Colors
-------------------------
red,orange,blue ,green

I will make it clear.

The table name is Colors
CREATE TABLE Colors
(
* * Color VARCHAR(32)
)

Values in the Table are
Color
------
red
orange
blue
green

My output should be when I do a Select * from Tabname, shd be

red,orange,blue ,green

Waiting for your reply

Thanks in Advancd
Mark:

Have a look at a posting to this newsgroup titled "concatenat ing
historical records" from October '07. In it, I listed two ways I know
of/have learned to do this.

--Jeff
Nov 19 '08 #3
On Nov 19, 3:53*pm, jefftyzzer <jefftyz...@sbc global.netwrote :
On Nov 19, 12:49*pm, mark83anth...@g mail.com wrote:


On Nov 19, 2:28*pm, mark83anth...@g mail.com wrote:
How do I concatenate strings from a column into a single row?
Whats the logic to create the function in DB2.
Given is below.
Color
------
red
orange
blue
green
And return a resultset like this:
Colors
-------------------------
red,orange,blue ,green
I will make it clear.
The table name is Colors
CREATE TABLE Colors
(
* * Color VARCHAR(32)
)
Values in the Table are
Color
------
red
orange
blue
green
My output should be when I do a Select * from Tabname, shd be
red,orange,blue ,green
Waiting for your reply
Thanks in Advancd

Mark:

Have a look at a posting to this newsgroup titled "concatenat ing
historical records" from October '07. In it, I listed two ways I know
of/have learned to do this.

--Jeff- Hide quoted text -

- Show quoted text -
Hi,

I tried doing this

SELECT replace(replace (xml2clob(xmlag g(xmlelement(NA ME a,
Color))),'<A>', ''),'</A>',' ')
FROM Colors;

It works fine with my DB2 9.5 client on my machine but does not work
with Db2 Z OS 9.1.

It gives me the following error message

SQL0171N The data type, length or value of argument "1" of routine
"REPLACE"
is incorrect. SQLSTATE=42815

SQL0171N The data type, length or value of argument "1" of routine
"REPLACE" is incorrect.

Explanation:

The data type, length or value of argument "<n>" of routine "<name>"
is
incorrect.

The statement cannot be processed.

User response:

Ensure the arguments of the routine conform to the rules of the
routine.

sqlcode: -171

sqlstate: 42815
Nov 19 '08 #4
On Nov 19, 2:22*pm, mark83anth...@g mail.com wrote:
On Nov 19, 3:53*pm, jefftyzzer <jefftyz...@sbc global.netwrote :
On Nov 19, 12:49*pm, mark83anth...@g mail.com wrote:
On Nov 19, 2:28*pm, mark83anth...@g mail.com wrote:
How do I concatenate strings from a column into a single row?
Whats the logic to create the function in DB2.
Given is below.
Color
------
red
orange
blue
green
And return a resultset like this:
Colors
-------------------------
red,orange,blue ,green
I will make it clear.
The table name is Colors
CREATE TABLE Colors
(
* * Color VARCHAR(32)
)
Values in the Table are
Color
------
red
orange
blue
green
My output should be when I do a Select * from Tabname, shd be
red,orange,blue ,green
Waiting for your reply
Thanks in Advancd
Mark:
Have a look at a posting to this newsgroup titled "concatenat ing
historical records" from October '07. In it, I listed two ways I know
of/have learned to do this.
--Jeff- Hide quoted text -
- Show quoted text -

Hi,

I tried doing this

SELECT replace(replace (xml2clob(xmlag g(xmlelement(NA ME a,
Color))),'<A>', ''),'</A>',' ')
FROM Colors;

It works fine with my DB2 9.5 client on my machine but does not work
with Db2 Z OS 9.1.

It gives me the following error message

SQL0171N *The data type, length or value of argument "1" of routine
"REPLACE"
is incorrect. *SQLSTATE=42815

SQL0171N *The data type, length or value of argument "1" of routine
"REPLACE" is incorrect.

Explanation:

The data type, length or value of argument "<n>" of routine "<name>"
is
incorrect.

The statement cannot be processed.

User response:

Ensure the arguments of the routine conform to the rules of the
routine.

*sqlcode: -171

*sqlstate: 42815
Well, notwithstanding a simple fix for the problem you're having with
the XML function, assuming that DB2 9.1 for Z/OS supports recursive
common table expressions (CTEs), you could try the other technique
demonstrated in the earlier posting I mentioned.

--Jeff
Nov 19 '08 #5
DB2 for z/OS doesn't support casting XML data type to another data
type.
(See Table 13. Supported casts between built-in data types
on Page 80 of "DB2 Version 9.1 for z/OS SQL Reference".)
Also, DB2 for z/OS doesn't support XML2CLOB function.
Nov 21 '08 #6
I have a Table called Colors both in Db2 9.5 for LUW and DB2 9.1 on Z/OS
SELECT replace(replace (xmlagg(xmlelem ent(NAME a,NAME)),'<A>', ' ' ),'</A>',' ') FROM TECPG01.TEST111 1

It works fine with db2 on LUW but generates an error for Db2 on z/OS
saying that argument 1 of the routine REPLACE is wrong.
It was neccesary to add an XML2CLOB function on DB2 9.1 for LUW, like
this:
SELECT replace(replace (XML2CLOB(xmlag g(xmlelement(NA ME a,
color))),'<A>', ''), '</A>', ' ') FROM Colors

I got error message SQL0440N for
SELECT replace(replace (xmlagg(xmlelem ent(NAME a, color)),'<A>', ''),
'</A>', ' ') FROM Colors

SQL0440N No authorized routine named "REPLACE" of type "FUNCTION"
having compatible arguments was found.
Nov 21 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
22851
by: Jay Chan | last post by:
I am trying to export data from a SQLServer database into a text file using a stored procedure. I want to be able to read it and debug it easily; therefore, I want all the columns to indent nicely. This means I need to append trailing spaces to a text string (such as "Test1 ") or append leading space in front of a text string that contains a number (such as " 12.00"). Now, the stored procedure works fine when I run it in Query Analyzer....
2
56066
by: SWN | last post by:
hi. Trying to concatenate two columns: select uname+' '+uaddress as NameAdr from tblUser I only get the first field, name!!?? The datatype is both nvarchar. 100 and 50 chars long. If I run this query:
3
10542
by: Lars Tackmann | last post by:
Hi - I need a function to concatenate a variable number of strings. I have two ideas but cannot deside which way to go. 1) use the "stdarg" macros - if i use these macros it will be easy to step through the strings, but as I see it (and I may be wrong here)I need to pass the number of strings to the function - which is difficult in this context. 2) using a pointer array as argument to the function. Here the length is again a problem -...
13
2380
by: jt | last post by:
I can't seem to find a way to concatenate strings that have nulls within the string. I have a string that I need another string that has nulls in it and what to append the 2nd string, 3 string and so forth to the 1st string. Any ideas how to go about this? Thanks,
5
3551
by: Diego Martins | last post by:
Since C++ (and STL) have many ways to do string concatenation, I want to hear (read) from you how you do to concatenate strings with other strings and other types. The approaches I know are: -- ostringstream this does the job well, but: * all types involved have to support operator<< * we will lose some readibility in the code because we will always have to create a temp object to do the concatenation
9
1748
by: Dave-o | last post by:
New learner. For the print command and concatenation, the period is difficult for my afflicted eyes to see, but the "+" seems to work. Is this an accepted PHP convention? Thank you, David
4
2469
Markus
by: Markus | last post by:
Hi, guys. I'm receiving a dynamic amount of strings and from these strings I need to make a single string containing them all. However, I need to preserve the NULL characters to separate the strings. If you see this page of documentation and scroll down to the REG_MULTI_SZ entry, you'll see the layout of the string needed. My thoughts on how I would go about this are: var full_string // empty foreach strings as string: ...
0
8987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9534
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8239
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.