I need help creating a delimitted string in DB2

346195 pts.
Tags:
IBM DB2
IBM iSeries
T-SQL
Over the past few days, I've been trying to create a delimited string from the results of a query in DB2 on iSeries. I was able to do it in T-SQL but am struggling here.

Here's the code I used in T-SQL. I'm basically looking for the equivalent in DB2.
DECLARE @a VARCHAR(1000)
SELECT @a = COALESCE(@a + ', ' + [Description], [Description])
FROM AP.Checkbooks
SELECT @a
Key: If the descriptions look like this in the table:
Desc 1
Desc 2
Desc 3
Then it should return this:
Desc 1, Desc 2, Desc 3

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • TomLiotta

    T-SQL can make it easier, but that's often the reason for using various programming and scripting languages. There are a number of possible languages you could use on AS/400s. However, first it's not clear what you want to do with the result. Printing would be different from displaying in a workstation panel, would be different from listing in a remote browser, would be different from using as input to an INSERT statement, etc. Second, is this code to run on the server or on some client? Third, what is the DB2 release? (Later releases always have more functions.)

    I would assume that you want to know how to do it in standard SQL rather than some secondary language. Generally this would be done with a recursive SQL statement. (See Using recursive queries.) The 'recursion' allows holding a previous result and combining it with each new row, usually by concatenation. A clear example might be given if the actual relevant parts of your table definition were included, especially key columns and search conditions.

    Tom

    125,585 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following