IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Date conversion in unload of data from DB2 to flat file


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
rajendraa

New User


Joined: 22 Dec 2006
Posts: 7
Location: Bangalore

PostPosted: Thu May 12, 2011 8:04 pm
Reply with quote

Hi,

I have a requirement to unload data from DB2 table to flat file using batch db2 unload utility

The file length is 50 bytes

Following are the columns that I will be downloading

Code:
Column Name:   Office_code   Start_Date
-------------------------------------------
Declaration:   CHAR(3)      DATE
-------------------------------------------
row1        :   USA      2010-05-30
row2        :   IND      2010-07-17


Data should be as follows in the flat file.
Code:
Office_code     PIC X(03)
Filler          PIC X(27) VALUE SPACES
Date            PIC X(08) (Format of YYYYMMDD)
Filler          PIC X(12)  VALUES SPAVES

Code:
----+----1----+----2----+----3----+----4----+----5
***************************** Top of Data ********
USA                          20100530             
IND                          20100717           



SELECT office_code
,CHAR(SPACE(27))
,start_date
,CHAR(space(12))
FROM TABLE1


Can someone help me how to convert date from YYYY-MM-DD format to YYYYMMDD format while downloading from DB2 to flat file?

Thank you
Back to top
View user's profile Send private message
haimzeevi

New User


Joined: 01 Mar 2010
Posts: 27
Location: Israel

PostPosted: Thu May 12, 2011 8:26 pm
Reply with quote

Hi,
Just add a SORT step, SYNCSORT or DFSORT, according your site, then use OUTREC statement to re-format the date-fields.
Works, fast & efficient.
If you have SYNCSORT, you can issue a direct SELECT from DB2, then use OUTREC to format the date-fields.
Haim Zeevi
Back to top
View user's profile Send private message
rajendraa

New User


Joined: 22 Dec 2006
Posts: 7
Location: Bangalore

PostPosted: Thu May 12, 2011 8:37 pm
Reply with quote

Thanks haimzeevi

I wanted to avoid sort step if the date conversion can be handeled in the unload query itself.

We can use replace and trim options in the unload select statement, but wanted to check if there is any better of doing this.
Back to top
View user's profile Send private message
Kjeld

Active User


Joined: 15 Dec 2009
Posts: 365
Location: Denmark

PostPosted: Thu May 12, 2011 8:48 pm
Reply with quote

If a local date format that suits your needs hasn't been implemented at your installation, I would suggest you look into selecting YYYY, MM, and DD separately with suitable date functions.
Back to top
View user's profile Send private message
rajendraa

New User


Joined: 22 Dec 2006
Posts: 7
Location: Bangalore

PostPosted: Thu May 12, 2011 11:28 pm
Reply with quote

Thanks Kjeld.

I tried as per your suggestion, but the zeros is getting replaced with spaces

Below is the query that was used

Select
CAST(YEAR(START_DATE) AS CHAR(4))
CAST(MONTH(START_DATE) AS CHAR(2))
CAST(DAY(START_DATE) AS CHAR(2))
from TABLE1

Value in DB2 table is 2010-05-30

Value in flat files after unload 2010 530

But I am looking for data as 20100530 in the flat file.

For month I am getting ' 5' instead of '05'. Is there a way to handle this.

Please advice.
Back to top
View user's profile Send private message
Akatsukami

Global Moderator


Joined: 03 Oct 2009
Posts: 1788
Location: Bloomington, IL

PostPosted: Fri May 13, 2011 12:07 am
Reply with quote

rajendraa wrote:
I tried as per your suggestion, but the zeros is getting replaced with spaces

Below is the query that was used

Select
CAST(YEAR(START_DATE) AS CHAR(4))
CAST(MONTH(START_DATE) AS CHAR(2))
CAST(DAY(START_DATE) AS CHAR(2))
from TABLE1

Value in DB2 table is 2010-05-30

Value in flat files after unload 2010 530

But I am looking for data as 20100530 in the flat file.

For month I am getting ' 5' instead of '05'. Is there a way to handle this.

Please advice.

Yes.














OK, I'll take pity on one of such limited ability: use the SQL TRANSLATE function. In fact, since you're evidently incapable of using Google, I'll even provide the link for you.
Back to top
View user's profile Send private message
rajendraa

New User


Joined: 22 Dec 2006
Posts: 7
Location: Bangalore

PostPosted: Fri May 13, 2011 1:33 am
Reply with quote

I used the below query and it solved my problem. Thanks all.
Select
SUBSTR(CHAR(START_DATE),1,4)
SUBSTR(CHAR(START_DATE),6,2)
SUBSTR(CHAR(START_DATE),9,2)
from TABLE1
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Fri May 13, 2011 12:07 pm
Reply with quote

two other ways to accomplish the same:
Code:
,char(varchar_format(timestamp(current date,'00.00.00'),'YYYYMMDD'),8)
,decimal(year(current date)*10000+month(current date)*100+day(current date),8,0)
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Binary File format getting change whi... All Other Mainframe Topics 7
No new posts How to save SYSLOG as text data via P... All Other Mainframe Topics 4
No new posts Compare 2 files and retrive records f... DFSORT/ICETOOL 3
No new posts FTP VB File from Mainframe retaining ... JCL & VSAM 8
No new posts Replacing 'YYMMDD' with date, varying... SYNCSORT 3
Search our Forums:

Back to Top