If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Format SQL Output

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-17-07, 12:14
koganti koganti is offline
Registered User
 
Join Date: Jul 2002
Location: ATL
Posts: 170
Format SQL Output

I want to write a sql to select rows from a table. I would like the output to have each column value separated by a comma in each row.

Iam trying the following sql and it does not seem to work. What am i doing wrong

db2 -x "select TABSCHEMA||','||card from syscat.tables"

I keep getting the follwoing error
SQL0440N No authorized routine named "||" of type "FUNCTION" having
compatible arguments was found. SQLSTATE=42884

Thx ahead
Reply With Quote
  #2 (permalink)  
Old 07-17-07, 12:27
koganti koganti is offline
Registered User
 
Join Date: Jul 2002
Location: ATL
Posts: 170
Format SQL in UDB

I want to write a sql to select rows from a table. I would like the output to have each column value separated by a comma in each row.

Iam trying the following sql and it does not seem to work. What am i doing wrong

db2 -x "select TABSCHEMA||','||card from syscat.tables"

I keep getting the follwoing error
SQL0440N No authorized routine named "||" of type "FUNCTION" having
compatible arguments was found. SQLSTATE=42884

Thx ahead
Reply With Quote
  #3 (permalink)  
Old 07-17-07, 12:30
nidm nidm is offline
Registered User
 
Join Date: May 2003
Posts: 113
Quote:
Originally Posted by koganti
I want to write a sql to select rows from a table. I would like the output to have each column value separated by a comma in each row.

Iam trying the following sql and it does not seem to work. What am i doing wrong

db2 -x "select TABSCHEMA||','||card from syscat.tables"

I keep getting the follwoing error
SQL0440N No authorized routine named "||" of type "FUNCTION" having
compatible arguments was found. SQLSTATE=42884

Thx ahead

to use '||' all the argument should be string. what's the datatype of column 'card'?
you can try this TABSCHEMA||','||varchar(card)
Reply With Quote
  #4 (permalink)  
Old 07-17-07, 12:31
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
DB2, you say? I don't know anything about it, but - is there the double pipe operator for concatenation in DB2? It exists in Oracle (that's for sure), but in DB2?

How about using CONCAT instead? Something like
Code:
SELECT tabschema CONCAT ',' CONCAT card
FROM syscat.tables
Reply With Quote
  #5 (permalink)  
Old 07-17-07, 12:58
koganti koganti is offline
Registered User
 
Join Date: Jul 2002
Location: ATL
Posts: 170
Following solved the problem
db2 -x "select tabschema||','||varchar(char(card)) from syscat.tables"

When you concatenate two fields then they should have same data type. If they are different data type then convert them to same data type.

Thx
Koganti
Reply With Quote
  #6 (permalink)  
Old 07-17-07, 13:37
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
Ah, lovely!

But, do you really need VARCHAR(CHAR(card)) ? Once again - I don't know DB2 SQL, but - wouldn't VARCHAR(card) or even CHAR(card) be enough?
Reply With Quote
  #7 (permalink)  
Old 07-17-07, 14:02
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
koganti, please don't double-post

i've merged your two threads, and you can see how much confusion you caused
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 07-17-07, 21:28
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
You can also use the cast function:

db2 -x "select tabschema||','|| cast(card as char(10)) from syscat.tables"

CHAR and VARCHAR can be concatenated.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #9 (permalink)  
Old 07-18-07, 06:20
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
|| is the standard SQL concatenation operator. CONCAT can also be used as synonym.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #10 (permalink)  
Old 07-18-07, 16:43
koganti koganti is offline
Registered User
 
Join Date: Jul 2002
Location: ATL
Posts: 170
For Varchar function in UDB you cannot pass numeric. You can only pass char or date.

Following worked
db2 -x "select tabschema||','|| cast(card as char(10)) from syscat.tables"

Thanks guys. You guys are awesome
Reply With Quote
  #11 (permalink)  
Old 07-20-07, 16:47
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
I would do this:
Code:
LTRIM(CHAR(number))
(or RTRIM - I can't remember which one is the right one)

This will get rid of leading spaces.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #12 (permalink)  
Old 07-23-07, 01:56
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Hi,
LTRIM - left trim (leading spaces)
RTRIM -right trim (spaces at the end of string)

In v9 there is also a TRIM function that trims left and right spaces.
Hope this helps,
Grofaty
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On