MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Convert all columns in SELECT * to CHAR

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
DiscoStuLikesDiscoMusic
Beginner


Joined: 15 May 2008
Posts: 5
Topics: 3

PostPosted: Tue Jul 27, 2010 11:09 am    Post subject: Convert all columns in SELECT * to CHAR Reply with quote

In a DB2 query, I need to convert columns of decimal data type to character so the DB2 data type will match the receiving COBOL field.

An example would be:

SELECT CHAR(COL_NAME_DECIMAL)
INTO :CHAR-DEFINED-FIELD
FROM TABLE_NAME

Where I run into a problem is that I'd like to use "SELECT *" rather than specify column names. I'm trying to place the rows of data into a generic COBOL table made up of character fields. Most of the data on the DB2 table is defined as CHAR, but there is a DECIMAL value here and there. Everything works fine until I run into the decimal data. In that case, I receive SQLCODE -303, Error: A VALUE CANNOT BE ASSIGNED TO OUTPUT HOST VARIABLE NUMBER BECAUSE THE DATA TYPES ARE NOT COMPARABLE .

Using "SELECT *" rather than specifying all the fields will allow me to reuse the same generic code for multiple DB2 tables. It will also allow me to avoid making changes in the future due to any modifications in the table structure (column name change, deleted/added column, etc).


Any ideas on how to place this CHAR/DECIMAL data into character defined fields using SELECT *?
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12368
Topics: 75
Location: San Jose

PostPosted: Tue Jul 27, 2010 12:36 pm    Post subject: Reply with quote

DiscoStuLikesDiscoMusic,

Here is an untested SQL which will generate a SQL select statement checking the property of the column from sysibm.syscolumns table.

Code:

SELECT A.COL_NAME                                                 
 FROM (SELECT SUBSTR(CHAR(CASE                                     
                     WHEN COLNO        = 1 AND                     
                          COLTYPE NOT IN ('FLOAT'                 
                                         ,'SMALLINT'               
                                         ,'INTEGER'               
                                         ,'DECIMAL')               
                     THEN CHAR('SELECT ') || CHAR(NAME,30)         
                     WHEN COLNO        > 1 AND                     
                          COLTYPE NOT IN ('FLOAT'                 
                                         ,'SMALLINT'               
                                         ,'INTEGER'               
                                         ,'DECIMAL')               
                     THEN CHAR('      ,') || CHAR(NAME,30)         
                     WHEN COLNO        = 1 AND                     
                          COLTYPE     IN ('FLOAT'                 
                                         ,'SMALLINT'               
                                         ,'INTEGER'               
                                         ,'DECIMAL')               
                     THEN CHAR('SELECT CHAR(')                ||   
                          SUBSTR(NAME,1,LENGTH(RTRIM(NAME)))  ||   
                          CHAR(')')                               
                     WHEN COLNO        > 1 AND                     
                          COLTYPE     IN ('FLOAT'                 
                                         ,'SMALLINT'               
                                         ,'INTEGER'               
                                         ,'DECIMAL')               
                     THEN CHAR('      ,CHAR(')                ||   
                          SUBSTR(NAME,1,LENGTH(RTRIM(NAME)))  ||   
                          CHAR(')')                               
                     END),1,80)              AS COL_NAME           
              ,COLNO                         AS COL_NO             
  FROM SYSIBM.SYSCOLUMNS                                           
 WHERE TBNAME = 'Your table name'                                     
 UNION                                                         
 SELECT B.COL_NAME                                             
       ,B.COL_NO                                               
   FROM (SELECT CHAR('  FROM TABLE_NAME')    AS COL_NAME       
               ,MAX(COLNO) + 1               AS COL_NO         
          FROM SYSIBM.SYSCOLUMNS                               
         WHERE TBNAME = 'your table name') B) A                   
 ORDER BY A.COL_NO                                             
 ;                                                             


Last edited by kolusu on Wed Jul 28, 2010 7:12 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 699
Topics: 63
Location: USA

PostPosted: Tue Jul 27, 2010 3:40 pm    Post subject: Reply with quote

Kolusu,

Did you miss
Code:

WHEN COLNO = 1 AND                                   
     COLTYPE     IN ('FLOAT'                         
                    ,'SMALLINT'                     
                    ,'INTEGER'                       
                    ,'DECIMAL')                     
 THEN                                               
   CHAR('SELECT SUBSTR(NAME,1,LENGTH(RTRIM(NAME)))')


And what is the ELSE for. Is it meant to handle the above case -

Code:
 ELSE CHAR(NAME,30)     

_________________
Regards,
Diba
Back to top
View user's profile Send private message Send e-mail
DiscoStuLikesDiscoMusic
Beginner


Joined: 15 May 2008
Posts: 5
Topics: 3

PostPosted: Tue Jul 27, 2010 4:40 pm    Post subject: Reply with quote

Perfect! Thank you.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12368
Topics: 75
Location: San Jose

PostPosted: Tue Jul 27, 2010 4:40 pm    Post subject: Reply with quote

Dibakar,

Sorry. Did not check it fully. I just tweaked an existing SQL and pasted it here without running. I guess I will miss if column 1 is a numeric column.

Thanks for catching it.

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 699
Topics: 63
Location: USA

PostPosted: Tue Jul 27, 2010 4:46 pm    Post subject: Reply with quote

You are welcome Kolusu. It's always fun to go through your replies.
_________________
Regards,
Diba
Back to top
View user's profile Send private message Send e-mail
computer
Beginner


Joined: 12 Jun 2007
Posts: 64
Topics: 17
Location: Hyderabad

PostPosted: Tue Sep 21, 2010 2:15 pm    Post subject: Reply with quote

Hi All,

I understood the first part of the query, which checks whether a column is of the float, decimal, smallint or integer and then converts into a char type.

Here, I have a question:
1. why a separate condition was given for '=1' and '>1', we can go ahead with '>=1'.

2. And what is the importance of 2nd query i.e. the one mentioned after UNION.

Thanks in Advance
Computer
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12368
Topics: 75
Location: San Jose

PostPosted: Tue Sep 21, 2010 2:26 pm    Post subject: Reply with quote

computer wrote:
Here, I have a question:
1. why a separate condition was given for '=1' and '>1', we can go ahead with '>=1'.


May be you need to see what is so different in output on the first line vs the rest of the lines.

Quote:

2. And what is the importance of 2nd query i.e. the one mentioned after UNION.


Remove the 2nd query and run the query and see what you are missing.

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group