Go Back   Rhinocerus > Newsgroup > Newsgroup comp.databases.* > Newsgroup comp.databases.ibm-db2

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 11-19-2008, 06:28 PM
mark83anthony@gmail.com
Guest
 
Posts: n/a
Default 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
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 11-19-2008, 07:49 PM
mark83anthony@gmail.com
Guest
 
Posts: n/a
Default Re: How do I concatenate strings from a column into a single row inDB2

On Nov 19, 2:28*pm, mark83anth...@gmail.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
Reply With Quote
  #3 (permalink)  
Old 11-19-2008, 07:53 PM
jefftyzzer
Guest
 
Posts: n/a
Default Re: How do I concatenate strings from a column into a single row inDB2

On Nov 19, 12:49*pm, mark83anth...@gmail.com wrote:
> On Nov 19, 2:28*pm, mark83anth...@gmail.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 "concatenating
historical records" from October '07. In it, I listed two ways I know
of/have learned to do this.

--Jeff
Reply With Quote
  #4 (permalink)  
Old 11-21-2008, 11:29 AM
Tonkuma
Guest
 
Posts: n/a
Default Re: How do I concatenate strings from a column into a single row inDB2

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.
Reply With Quote
  #5 (permalink)  
Old 11-21-2008, 11:52 AM
Tonkuma
Guest
 
Posts: n/a
Default Re: How do I concatenate strings from a column into a single row inDB2

> I have a Table called Colors both in Db2 9.5 for LUW and DB2 9.1 on Z/OS

> SELECT replace(replace(xmlagg(xmlelement(NAME a,NAME)),'<A>',' ' ),'</A>',' ') FROM TECPG01.TEST1111
>
> 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(xmlagg(xmlelement(NAME a,
color))),'<A>', ''), '</A>', ' ') FROM Colors

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

SQL0440N No authorized routine named "REPLACE" of type "FUNCTION"
having compatible arguments was found.
Reply With Quote
  #6 (permalink)  
Old 11-21-2008, 01:49 PM
mark83anthony@gmail.com
Guest
 
Posts: n/a
Default Re: How do I concatenate strings from a column into a single row inDB2

On Nov 21, 7:52*am, Tonkuma <tonk...@fiberbit.net> wrote:
> > I have a Table called Colors both in Db2 9.5 for LUW and DB2 9.1 on Z/OS
> > SELECT replace(replace(xmlagg(xmlelement(NAME a,NAME)),'<A>',' ' ),'</A>',' ') FROM TECPG01.TEST1111

>
> > 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(xmlagg(xmlelement(NAME a,
> color))),'<A>', ''), '</A>', ' ') FROM Colors
>
> I got error message SQL0440N for
> SELECT replace(replace(xmlagg(xmlelement(NAME a, color)),'<A>', ''),
> '</A>', ' ') FROM Colors
>
> SQL0440N *No authorized routine named "REPLACE" of type "FUNCTION"
> having compatible arguments was found.


Hi,

thanks for replying.

Yeah, u are true, It was necessary to use a xml2clob in order to get
the result on Db2 9.1 for LUW.

But using SELECT XMLAGG(XMLELEMENT(NAME a, NAME)) from
TECPG01.TEST1111 on Db2 9.1 for z/OS I was able to get the same
result as in using XML2CLOB with above for Db2 LUW.

I went over the syntax of Replace. replace function accepts only
expressions as arguments in Db2 for LUW
but Replace functions accepts only string aruguments in db2 for z/OS.

Does this anyway is effecting what I wanted to achieve.

If yes, How can I convert an expression to a string argument.

Reply With Quote
  #7 (permalink)  
Old 11-22-2008, 01:21 AM
Tonkuma
Guest
 
Posts: n/a
Default Re: How do I concatenate strings from a column into a single row inDB2

> Yeah, u are true, It was necessary to use a xml2clob in order to get
> the result on Db2 9.1 for LUW.
>
> But using SELECT XMLAGG(XMLELEMENT(NAME a, NAME)) from
> TECPG01.TEST1111 on Db2 9.1 for z/OS *I was able to get the same
> result as in using XML2CLOB with above for Db2 LUW.

I got same result by using Command Editor on DB2 LUW 9.1 and 9.5
without using XML2CLOB function.
But, the data type must be XML and I guessed that Command Editor
converted the result into displayable format.
Because, the statement "SELECT SUBSTR(XMLAGG(XMLELEMENT(NAME a, NAME)),
1,100) from Colors;", returned error message SQL0440N.
It is neccesary XML data to cast to string(I did it by using XML2CLOB)
to use the XML data as a string argument of DB2 functions like SUBSTR,
REPLACE, so on.

>
> I went over the syntax of Replace. replace function accepts only
> expressions as arguments in Db2 for LUW
> but Replace functions accepts only string aruguments in db2 for z/OS.
>

REPLACE on DB2 for LUW is almost same as REPLACE on DB2 for z/OS.

If you read the description of the function on SQL Reference of DB2
for LUW,
you can see:
The first argument can be of any built-in character string or binary
string type.
<snipped>
The type of the second and third arguments is identical to that of the
first argument.

> Does this anyway is effecting what I wanted to achieve.
>
> If yes, How can I convert an expression to a string argument.

As far as I know, there's no way to convert XML data to string on DB2
for z/OS by using bult-in functions.

Reply With Quote
  #8 (permalink)  
Old 11-24-2008, 02:54 PM
mark83anthony@gmail.com
Guest
 
Posts: n/a
Default Re: How do I concatenate strings from a column into a single row inDB2

On Nov 21, 9:21*pm, Tonkuma <tonk...@fiberbit.net> wrote:
> > Yeah, u are true, It was necessary to use a xml2clob in order to get
> > the result on Db2 9.1 for LUW.

>
> > But using SELECT XMLAGG(XMLELEMENT(NAME a, NAME)) from
> > TECPG01.TEST1111 on Db2 9.1 for z/OS *I was able to get the same
> > result as in using XML2CLOB with above for Db2 LUW.

>
> I got same result by using Command Editor on DB2 LUW 9.1 and 9.5
> without using XML2CLOB function.
> But, the data type must be XML and I guessed that Command Editor
> converted the result into displayable format.
> Because, the statement "SELECT SUBSTR(XMLAGG(XMLELEMENT(NAME a, NAME)),
> 1,100) from Colors;", returned error message SQL0440N.
> It is neccesary XML data to cast to string(I did it by using XML2CLOB)
> to use the XML data as a string argument of DB2 functions like SUBSTR,
> REPLACE, so on.
>
>
>
> > I went over the syntax of Replace. replace function accepts only
> > expressions as arguments in Db2 for LUW
> > but Replace functions accepts only string aruguments in db2 for z/OS.

>
> REPLACE on DB2 for LUW is almost same as REPLACE on DB2 for z/OS.
>
> If you read the description of the function on SQL Reference of DB2
> for LUW,
> you can see:
> The first argument can be of any built-in character string or binary
> string type.
> <snipped>
> The type of the second and third arguments is identical to that of the
> first argument.
>
> > Does this anyway is effecting what I wanted to achieve.

>
> > If yes, How can I convert an expression to a string argument.

>
> As far as I know, there's no way to convert XML data to string on DB2
> for z/OS by using bult-in functions.


Hi Tonkuma,

Thanks so much for replying.

I am a DB2 AIX guy, so fairly new to z/os.

I used recursive SQL to produce what i wanted to.

Below is the SQL.



WITH temp1(NAME, all_words, cnt) as
(SELECT min(NAME), VARCHAR(min(NAME),2000),SMALLINT(1) FROM
TECPG01.TEST1111 a
UNION ALL
SELECT a.NAME, b.all_words||','||a.NAME,SMALLINT(b.cnt+1)
FROM TECPG01.TEST1111 a, temp1 b
WHERE a.NAME > b.NAME
AND a.NAME = (select min(c.NAME) from TECPG01.TEST1111 c where c.NAME
> b.NAME)

)
SELECT all_words FROM temp1 d
where cnt = (SELECT max(cnt) FROM temp1)

Now, I decided to write a function and ask users to call this function
to concatenate multiple rows to a string.

How do I write a function using Recursive SQL for this,

Does DB2 for z OS support Recursive SQL.

I would be very thankful to you if you get me this function for me to
get going.

Thanks in Advance again.
Reply With Quote
  #9 (permalink)  
Old 11-24-2008, 07:47 PM
mark83anthony@gmail.com
Guest
 
Posts: n/a
Default Re: How do I concatenate strings from a column into a single row inDB2

On Nov 24, 10:54*am, mark83anth...@gmail.com wrote:
> On Nov 21, 9:21*pm, Tonkuma <tonk...@fiberbit.net> wrote:
>
>
>
>
>
> > > Yeah, u are true, It was necessary to use a xml2clob in order to get
> > > the result on Db2 9.1 for LUW.

>
> > > But using SELECT XMLAGG(XMLELEMENT(NAME a, NAME)) from
> > > TECPG01.TEST1111 on Db2 9.1 for z/OS *I was able to get the same
> > > result as in using XML2CLOB with above for Db2 LUW.

>
> > I got same result by using Command Editor on DB2 LUW 9.1 and 9.5
> > without using XML2CLOB function.
> > But, the data type must be XML and I guessed that Command Editor
> > converted the result into displayable format.
> > Because, the statement "SELECT SUBSTR(XMLAGG(XMLELEMENT(NAME a, NAME)),
> > 1,100) from Colors;", returned error message SQL0440N.
> > It is neccesary XML data to cast to string(I did it by using XML2CLOB)
> > to use the XML data as a string argument of DB2 functions like SUBSTR,
> > REPLACE, so on.

>
> > > I went over the syntax of Replace. replace function accepts only
> > > expressions as arguments in Db2 for LUW
> > > but Replace functions accepts only string aruguments in db2 for z/OS.

>
> > REPLACE on DB2 for LUW is almost same as REPLACE on DB2 for z/OS.

>
> > If you read the description of the function on SQL Reference of DB2
> > for LUW,
> > you can see:
> > The first argument can be of any built-in character string or binary
> > string type.
> > <snipped>
> > The type of the second and third arguments is identical to that of the
> > first argument.

>
> > > Does this anyway is effecting what I wanted to achieve.

>
> > > If yes, How can I convert an expression to a string argument.

>
> > As far as I know, there's no way to convert XML data to string on DB2
> > for z/OS by using bult-in functions.

>
> Hi Tonkuma,
>
> Thanks so much for replying.
>
> I am a DB2 AIX guy, so fairly new to z/os.
>
> I used recursive SQL to produce what i wanted to.
>
> Below is the SQL.
>
> WITH temp1(NAME, all_words, cnt) as
> (SELECT min(NAME), VARCHAR(min(NAME),2000),SMALLINT(1) FROM
> TECPG01.TEST1111 a
> UNION ALL
> SELECT a.NAME, b.all_words||','||a.NAME,SMALLINT(b.cnt+1)
> FROM TECPG01.TEST1111 a, temp1 b
> WHERE a.NAME > b.NAME
> AND a.NAME = (select min(c.NAME) from TECPG01.TEST1111 c where c.NAME> b.NAME)
>
> )
> SELECT all_words FROM temp1 d
> where cnt = (SELECT max(cnt) FROM temp1)
>
> Now, I decided to write a function and ask users to call this function
> to concatenate multiple rows to a string.
>
> How do I write a function using Recursive SQL for this,
>
> Does DB2 for z OS support Recursive SQL.
>
> I would be very thankful to you if you get me this function for me to
> get going.
>
> Thanks in Advance again.- Hide quoted text -
>
> - Show quoted text -


Or else,

Can i get an equivalent function/ stored procedure of the below
function in DB2 for z os.


CREATE FUNCTION get_str()
LANGUAGE SQL
RETURNS VARCHAR(1024)
BEGIN ATOMIC
DECLARE str VARCHAR(1024);
SET str = '';
loop1: FOR row AS (SELECT Name FROM TECPG01.TEST1111)
DO
IF row.Name IS NOT NULL THEN
SET str = str || row.Name || '';
END IF;
END FOR loop1;
RETURN str;
END@
Reply With Quote
  #10 (permalink)  
Old 07-30-2012, 05:11 AM
sahilrtlrtl@gmail.com
Guest
 
Posts: n/a
Default Re: How do I concatenate strings from a column into a single row in DB2

On Thursday, November 20, 2008 12:58:48 AM UTC+5:30, (unknown) 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



CREATE TABLE COLOR
(
COLORS VARCHAR(MAX)
)



CREATE FUNCTION [UDF_COL_TO_SINGLE_ROW]
(
@VALUE AS VARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE @colors NVARCHAR(50)

SELECT @colors = COALESCE(@colors + ',', '') + COLORS
FROM
COLOR

RETURN @colors
END

SELECT DBO.UDF_COL_TO_SINGLE_ROW ('DASD') AS COLOR_NAME


IT MUST WORKS.....ALL D BEST............



Reply With Quote
  #11 (permalink)  
Old 07-30-2012, 05:13 AM
sahilrtlrtl@gmail.com
Guest
 
Posts: n/a
Default Re: How do I concatenate strings from a column into a single row in DB2

On Thursday, November 20, 2008 2:19:23 AM UTC+5:30, (unknown) wrote:
> On Nov 19, 2:28*pm, mark83anth...@gmail.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






CREATE TABLE COLOR
(
COLORS VARCHAR(MAX)
)



CREATE FUNCTION [UDF_COL_TO_SINGLE_ROW]
(
@VALUE AS VARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE @colors NVARCHAR(50)

SELECT @colors = COALESCE(@colors + ',', '') + COLORS
FROM
COLOR

RETURN @colors
END

SELECT DBO.UDF_COL_TO_SINGLE_ROW ('DASD') AS COLOR_NAME


IT MUST WORKS.....ALL D BEST............
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Subtracting Values in a Single Column Howard Schreier Newsgroup comp.soft-sys.sas 0 04-29-2008 05:09 PM
Re: Subtracting Values in a Single Column Nat Wooding Newsgroup comp.soft-sys.sas 0 04-29-2008 01:13 PM
proc report need number to be date gscsrc@hotmail.com Newsgroup comp.soft-sys.sas 11 10-29-2007 03:46 PM
Re: How to build Dynamic Variable names and values Arthur Tabachneck Newsgroup comp.soft-sys.sas 0 02-11-2006 06:34 PM
Re: How to build Dynamic Variable names and values SUBSCRIBE SAS-L Chandra Gadde Newsgroup comp.soft-sys.sas 0 02-11-2006 03:50 PM



All times are GMT. The time now is 10:27 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.